List of usage examples for com.google.common.hash HashingInputStream HashingInputStream
public HashingInputStream(HashFunction hashFunction, InputStream in)
From source file:org.jclouds.io.ByteStreams2.java
public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException { checkNotNull(input, "input"); checkNotNull(hashFunction, "hashFunction"); try {//from w w w. j ava2s . c om HashingInputStream his = new HashingInputStream(hashFunction, input); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } finally { closeQuietly(input); } }
From source file:org.sonatype.nexus.common.hash.Hashes.java
/** * Computes the hash of the given stream using the given algorithm. *//* w w w . j a v a 2 s. c o m*/ public static HashCode hash(final HashAlgorithm algorithm, final InputStream inputStream) throws IOException { checkNotNull(algorithm); checkNotNull(inputStream); try (HashingInputStream hashingStream = new HashingInputStream(algorithm.function(), inputStream)) { ByteStreams.copy(hashingStream, ByteStreams.nullOutputStream()); return hashingStream.hash(); } }
From source file:com.lithium.flow.util.HashEncoder.java
@Nonnull public String process(@Nonnull InputStream in) throws IOException { checkNotNull(in);/*from www .j av a 2 s . co m*/ try { HashingInputStream hashIn = new HashingInputStream(function, in); IOUtils.copy(hashIn, ByteStreams.nullOutputStream()); return encoding.encode(hashIn.hash().asBytes()); } finally { IOUtils.closeQuietly(in); } }
From source file:net.minecrell.workbench.tools.DownloadTools.java
public static void download(URLConnection con, Path output, boolean verifyEtag) throws IOException { if (verifyEtag) { HashCode hash;//from www .ja v a 2 s . co m try (HashingInputStream in = new HashingInputStream(ETAG_HASH, con.getInputStream())) { download(in, output); hash = in.hash(); } String expected = getEtag(con); if (!expected.isEmpty()) { String result = hash.toString(); if (!result.equals(expected)) { Files.delete(output); throw new IOException("Checksum mismatch for " + output.getFileName().toString() + ": expected " + expected + " got " + result); } } } else { download(con.getInputStream(), output); } }
From source file:com.facebook.buck.io.HashingDeterministicJarWriter.java
public HashingDeterministicJarWriter writeEntry(String name, InputStream contents) throws IOException { try (HashingInputStream hashingContents = new HashingInputStream(Hashing.murmur3_128(), contents)) { writeToJar(name, hashingContents); manifestWriter.setEntryAttribute(name, DIGEST_ATTRIBUTE_NAME, hashingContents.hash().toString()); }//w w w .j av a 2 s . com return this; }
From source file:org.jclouds.glacier.util.TreeHash.java
/** * Builds the Hash and the TreeHash values of the payload. * * @return The calculated TreeHash.//from ww w .j a v a 2 s . com * @see <a href="http://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html" /> */ public static TreeHash buildTreeHashFromPayload(Payload payload) throws IOException { InputStream is = null; try { is = checkNotNull(payload, "payload").openStream(); Builder<HashCode> list = ImmutableList.builder(); HashingInputStream linearHis = new HashingInputStream(Hashing.sha256(), is); while (true) { HashingInputStream chunkedHis = new HashingInputStream(Hashing.sha256(), ByteStreams.limit(linearHis, CHUNK_SIZE)); long count = ByteStreams.copy(chunkedHis, ByteStreams.nullOutputStream()); if (count == 0) { break; } list.add(chunkedHis.hash()); } //The result list contains exactly one element now. return new TreeHash(hashList(list.build()), linearHis.hash()); } finally { closeQuietly(is); } }
From source file:org.openscoring.service.ModelRegistry.java
@SuppressWarnings(value = { "resource" }) public Model load(InputStream is) throws Exception { CountingInputStream countingIs = new CountingInputStream(is); HashingInputStream hashingIs = new HashingInputStream(Hashing.md5(), countingIs); ModelEvaluator<?> evaluator = unmarshal(hashingIs, this.validate); PMML pmml = evaluator.getPMML();//from w w w .ja v a 2 s.co m for (Class<? extends Visitor> visitorClazz : this.visitorClazzes) { Visitor visitor = visitorClazz.newInstance(); visitor.applyTo(pmml); } evaluator.verify(); Model model = new Model(evaluator); model.putProperty(Model.PROPERTY_FILE_SIZE, countingIs.getCount()); model.putProperty(Model.PROPERTY_FILE_MD5SUM, (hashingIs.hash()).toString()); return model; }
From source file:com.facebook.buck.jvm.java.JarDumper.java
private Stream<String> dumpBinaryFile(String name, InputStream inputStream) throws IOException { try (HashingInputStream is = new HashingInputStream(Hashing.murmur3_128(), inputStream)) { ByteStreams.exhaust(is);//from ww w . jav a2 s . c o m return Stream.of(String.format("Murmur3-128 of %s: %s", name, is.hash().toString())); } }
From source file:org.jclouds.jdbc.service.JdbcService.java
@Transactional(rollbackOn = IOException.class) public BlobEntity createOrModifyBlob(String containerName, Blob blob, BlobAccess blobAccess) throws IOException { List<Long> chunks; HashingInputStream his = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream()); try {//from w ww . ja va 2s. c o m chunks = storeData(his); } finally { Closeables2.closeQuietly(his); } HashCode actualHashCode = his.hash(); HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { throw new IOException( "MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode); } String key = blob.getMetadata().getName(); Date creationDate = null; BlobEntity oldBlobEntity = findBlobById(containerName, key); if (oldBlobEntity != null) { creationDate = oldBlobEntity.getCreationDate(); } BlobEntity blobEntity = blobToBlobEntity.apply(blob); blobEntity.getPayload().setChunks(chunks); blobEntity.setContainerEntity(containerRepository.findContainerByName(containerName)); blobEntity.setKey(key); blobEntity.setBlobAccess(blobAccess); blobEntity.setTier(blob.getMetadata().getTier()); blobEntity.setCreationDate(creationDate); blobEntity.setLastModified(new Date()); blobEntity.setEtag(base16().lowerCase().encode(actualHashCode.asBytes())); blobEntity.getPayload().setContentMD5(actualHashCode.asBytes()); BlobEntity result = blobRepository.save(blobEntity); return result; }
From source file:org.jclouds.glacier.util.AWSRequestSignerV4.java
private static HashCode buildHashedPayload(HttpRequest request) { HashingInputStream his = null;/*from ww w . j av a 2s .c o m*/ try { his = new HashingInputStream(Hashing.sha256(), request.getPayload() == null ? ByteSource.empty().openStream() : request.getPayload().openStream()); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } catch (IOException e) { throw new HttpException("Error signing request", e); } finally { closeQuietly(his); } }