Package com.google.common.hash

Examples of com.google.common.hash.Hasher


    }

    @Override
    public String write(InputStream in) throws MicroKernelException {
        try {
            final Hasher hasher = Hashing.sha256().newHasher();
            Blob blob = store.createBlob(
                    new CheckedInputStream(in, new Checksum() {
                        @Override
                        public void update(byte[] b, int off, int len) {
                            hasher.putBytes(b, off, len);
                        }
                        @Override
                        public void update(int b) {
                            hasher.putByte((byte) b);
                        }
                        @Override
                        public void reset() {
                            throw new UnsupportedOperationException();
                        }
                        @Override
                        public long getValue() {
                            throw new UnsupportedOperationException();
                        }
                    }));
            HashCode hash = hasher.hash();

            // wrap into AbstractBlob for the SHA-256 memorization feature
            if (!(blob instanceof AbstractBlob)) {
                final Blob b = blob;
                blob = new AbstractBlob(hash) {
View Full Code Here


   * Hashes the contents of this byte source using the given hash function.
   *
   * @throws IOException if an I/O error occurs in the process of reading from this source
   */
  public HashCode hash(HashFunction hashFunction) throws IOException {
    Hasher hasher = hashFunction.newHasher();
    copyTo(Funnels.asOutputStream(hasher));
    return hasher.hash();
  }
View Full Code Here

    }

    @Override
    public int hashCode() {
        HashFunction hf = Hashing.goodFastHash(32);
        Hasher h = hf.newHasher();
        h.putInt(slots.size());
        for (int i=0; i<slots.size(); i++) {
            h.putInt(slots.get(i).size());
            for (int j=0; j<slots.size(); j++) {
                h.putBytes(slots.get(i).get(j).getLowerRange());
                h.putBytes(slots.get(i).get(j).getUpperRange());
            }
        }
        return h.hash().asInt();
    }
View Full Code Here

            log.info("/proc/cpuinfo file not found; won't include in machine signature");
        }

        Collections.sort(material);

        Hasher hasher = Hashing.md5().newHasher();
        for (String s : material) {
            hasher.putString(s);
            hasher.putByte((byte) 0);
        }

        return hostname + "_" + hasher.hash().toString();
    }
View Full Code Here

            // We need something that can be consistent
            // Some people say it's OK to just use the hash of the key,
            // but obviously there's no proof of that. We add a little
            // complexity
            // to be safer.
            Hasher hasher = Hashing.sha256().newHasher();
            byte[] passwordBytes = password.getBytes(Charsets.UTF_8);
            hasher.putBytes(passwordBytes);
            hasher.putBytes(aesBytes);
            hasher.putBytes(passwordBytes);
            hasher.putBytes(salt);
            hasher.putBytes(passwordBytes);
            hmacBytes = hasher.hash().asBytes();
        }
        if (hmacBytes.length != (256 / 8)) {
            throw new IllegalStateException();
        }
        try {
View Full Code Here

        }

        try (TempFile tempFile = TempFile.create()) {
            FileRange.Builder c = FileRange.newBuilder();

            Hasher md5 = Hashing.md5().newHasher();
            try (OutputStream fos = new HashingOutputStream(new FileOutputStream(tempFile.getFile()), md5)) {
                for (int i = bestStart; i < bestEnd; i++) {
                    FileRange range = file.getRanges(i);
                    if (i == bestStart) {
                        c.setStart(range.getStart());
                    }
                    if (i == (bestEnd - 1)) {
                        c.setEnd(range.getEnd());
                    }

                    final BlobData blob = blobStore.find(range.getContentKey());
                    if (blob == null) {
                        throw new IOException("Unable to open storage for range: " + range);
                    }

                    blob.copyTo(fos);
                }
            }

            if (!c.hasStart() || !c.hasEnd()) {
                throw new IllegalStateException();
            }

            ByteString hash = ByteString.copyFrom(md5.hash().asBytes());
            BlobData blobData = new BlobData(tempFile.getFile(), hash);
            blobStore.put(blobData);

            if (PARANOID) {
                BlobData blob = blobStore.find(blobData.getHash());
View Full Code Here

        storeDirectory(getDirectoryStorage(project), newDir);
        return true;
    }

    public static void sanityCheck(BlobStore blobStore, FileData.Builder file) {
        Hasher md5 = Hashing.md5().newHasher();
        try (HashingOutputStream hos = new HashingOutputStream(ByteStreams.nullOutputStream(), md5)) {
            List<FileRange> ranges = file.getRangesList();
            for (int i = 0; i < ranges.size(); i++) {
                FileRange range = ranges.get(i);

                BlobData blob = blobStore.find(range.getContentKey());
                if (blob == null) {
                    throw new IllegalStateException("Unable to find blob for range: " + range);
                }

                log.debug("Sanity check: fetch blob {}", Hex.toHex(range.getContentKey().toByteArray()));
                if (blob.size() != (range.getEnd() - range.getStart())) {
                    throw new IllegalStateException();
                }

                blob.copyTo(hos);

                if (i != 0) {
                    FileRange prev = ranges.get(i - 1);

                    if (prev.getEnd() != range.getStart()) {
                        throw new IllegalStateException();
                    }
                } else {
                    if (range.getStart() != 0) {
                        throw new IllegalStateException();
                    }
                }

                if (i == (ranges.size() - 1)) {
                    if (range.getEnd() != file.getLength()) {
                        throw new IllegalStateException();
                    }
                }
            }
        } catch (IOException e) {
            throw new IllegalStateException("Error checking file: " + file, e);
        }

        ByteString hash = ByteString.copyFrom(md5.hash().asBytes());

        if (!file.getHash().equals(hash)) {
            log.warn("Hash mismatch: {} vs {}", Hex.toHex(file.getHash().toByteArray()), Hex.toHex(hash.toByteArray()));
            throw new IllegalStateException("Hash mismatch");
        }
View Full Code Here

        // TODO: Any more for the mix??

        material.add(computeFileHash("/proc/cpuinfo"));
        Collections.sort(material);

        Hasher hasher = Hashing.md5().newHasher();
        for (String s : material) {
            hasher.putString(s);
            hasher.putByte((byte) 0);
        }

        return hostname + "_" + hasher.hash().toString();
    }
View Full Code Here

            try (TempFile localTemp = TempFile.in(this.localCacheDirTmp)) {
                HashCode md5;

                try (Sftp sftp = buildSftp(); InputStream is = sftp.open(remoteFile.getSshPath())) {
                    try (OutputStream os = new FileOutputStream(localTemp.getFile())) {
                        Hasher hasher = Hashing.md5().newHasher();
                        byte[] buffer = new byte[8192];
                        while (true) {
                            int n = is.read(buffer);
                            if (n == -1) {
                                break;
                            }

                            hasher.putBytes(buffer, 0, n);
                            os.write(buffer, 0, n);
                        }
                        md5 = hasher.hash();
                    }
                } catch (FileNotFoundException e) {
                    // Unable to find sftp file
                    return null;
                }
View Full Code Here

   * Hashes the contents of this byte source using the given hash function.
   *
   * @throws IOException if an I/O error occurs in the process of reading from this source
   */
  public HashCode hash(HashFunction hashFunction) throws IOException {
    Hasher hasher = hashFunction.newHasher();
    copyTo(Funnels.asOutputStream(hasher));
    return hasher.hash();
  }
View Full Code Here

TOP

Related Classes of com.google.common.hash.Hasher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.