public void testHash_hashesCorrectly() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectlyForSkipping() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'}); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); long numOfByteSkipped = in.skip(2); assertEquals(2, numOfByteSkipped); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(2, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectlyReadOutOfBound() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[100]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(-1, in.read()); // additional read assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException { checkNotNull(input, "input"); checkNotNull(hashFunction, "hashFunction"); try { HashingInputStream his = new HashingInputStream(hashFunction, input); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } finally { closeQuietly(input); } }
public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException { checkNotNull(input, "input"); checkNotNull(hashFunction, "hashFunction"); try { HashingInputStream his = new HashingInputStream(hashFunction, input); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } finally { closeQuietly(input); } }
public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException { checkNotNull(input, "input"); checkNotNull(hashFunction, "hashFunction"); try { HashingInputStream his = new HashingInputStream(hashFunction, input); ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash(); } finally { closeQuietly(input); } }
public static void install(Project project) { File f = project.file(PUPPET_PATH); try (InputStream in = InstallPuppet.class.getResourceAsStream("/unityPuppet.dll")) { Log.L.info("Unity puppet installed {}", f.exists()); if (f.exists()) { HashCode currentHash = Files.hash(f, Hashing.md5()); HashCode newHash = new HashingInputStream(Hashing.md5(), in).hash(); boolean upToDate = currentHash.equals(newHash); Log.L.info("Unity puppet up to date {}", upToDate); if (upToDate) { return; } } Files.createParentDirs(f); try (FileOutputStream o = new FileOutputStream(f)) { ByteStreams.copy(in, o); } } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * hash input with sha256 * * @param input * @return hash result * @throws HttpException */ public static byte[] hash(InputStream input) throws HttpException { HashingInputStream his = new HashingInputStream(Hashing.sha256(), input); try { ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash().asBytes(); } catch (IOException e) { throw new HttpException("Unable to compute hash while signing request: " + e.getMessage(), e); } }
/** * hash input with sha256 * * @param input * @return hash result * @throws HTTPException */ public static byte[] hash(InputStream input) throws HTTPException { HashingInputStream his = new HashingInputStream(Hashing.sha256(), input); try { ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash().asBytes(); } catch (IOException e) { throw new HttpException("Unable to compute hash while signing request: " + e.getMessage(), e); } }
/** * hash input with sha256 * * @param input * @return hash result * @throws HttpException */ public static byte[] hash(InputStream input) throws HttpException { HashingInputStream his = new HashingInputStream(Hashing.sha256(), input); try { ByteStreams.copy(his, ByteStreams.nullOutputStream()); return his.hash().asBytes(); } catch (IOException e) { throw new HttpException("Unable to compute hash while signing request: " + e.getMessage(), e); } }
@Override public String putBlob(final String containerName, final Blob blob) throws IOException { byte[] payload; HashCode actualHashCode; HashingInputStream input = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream()); try { payload = ByteStreams.toByteArray(input); long actualSize = payload.length; Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength(); if (expectedSize != null && actualSize != expectedSize) { throw new IOException("Content-Length mismatch, actual: " + actualSize + " expected: " + expectedSize); } actualHashCode = input.hash(); HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { throw new IOException("MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode); } } finally { Closeables2.closeQuietly(input); } Blob newBlob = createUpdatedCopyOfBlobInContainer(containerName, blob, payload, actualHashCode); Map<String, Blob> map = containerToBlobs.get(containerName); String blobName = newBlob.getMetadata().getName(); map.put(blobName, newBlob); containerToBlobAccess.get(containerName).put(blobName, BlobAccess.PRIVATE); return base16().lowerCase().encode(actualHashCode.asBytes()); }
@Override public String putBlob(final String containerName, final Blob blob) throws IOException { byte[] payload; HashCode actualHashCode; HashingInputStream input = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream()); try { payload = ByteStreams.toByteArray(input); long actualSize = payload.length; Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength(); if (expectedSize != null && actualSize != expectedSize) { throw new IOException("Content-Length mismatch, actual: " + actualSize + " expected: " + expectedSize); } actualHashCode = input.hash(); HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { throw new IOException("MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode); } } finally { Closeables2.closeQuietly(input); } Blob newBlob = createUpdatedCopyOfBlobInContainer(containerName, blob, payload, actualHashCode); Map<String, Blob> map = containerToBlobs.get(containerName); String blobName = newBlob.getMetadata().getName(); map.put(blobName, newBlob); containerToBlobAccess.get(containerName).put(blobName, BlobAccess.PRIVATE); return base16().lowerCase().encode(actualHashCode.asBytes()); }
@Override public String putBlob(final String containerName, final Blob blob) throws IOException { byte[] payload; HashCode actualHashCode; HashingInputStream input = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream()); try { payload = ByteStreams.toByteArray(input); long actualSize = payload.length; Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength(); if (expectedSize != null && actualSize != expectedSize) { throw new IOException("Content-Length mismatch, actual: " + actualSize + " expected: " + expectedSize); } actualHashCode = input.hash(); HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { throw new IOException("MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode); } } finally { Closeables2.closeQuietly(input); } Blob newBlob = createUpdatedCopyOfBlobInContainer(containerName, blob, payload, actualHashCode); Map<String, Blob> map = containerToBlobs.get(containerName); String blobName = newBlob.getMetadata().getName(); map.put(blobName, newBlob); containerToBlobAccess.get(containerName).put(blobName, BlobAccess.PRIVATE); return base16().lowerCase().encode(actualHashCode.asBytes()); }
@Override public String putBlob(final String containerName, final Blob blob) throws IOException { byte[] payload; HashCode actualHashCode; HashingInputStream input = new HashingInputStream(Hashing.md5(), blob.getPayload().openStream()); try { payload = ByteStreams.toByteArray(input); long actualSize = payload.length; Long expectedSize = blob.getMetadata().getContentMetadata().getContentLength(); if (expectedSize != null && actualSize != expectedSize) { throw new IOException("Content-Length mismatch, actual: " + actualSize + " expected: " + expectedSize); } actualHashCode = input.hash(); HashCode expectedHashCode = blob.getPayload().getContentMetadata().getContentMD5AsHashCode(); if (expectedHashCode != null && !actualHashCode.equals(expectedHashCode)) { throw new IOException("MD5 hash code mismatch, actual: " + actualHashCode + " expected: " + expectedHashCode); } } finally { Closeables2.closeQuietly(input); } Blob newBlob = createUpdatedCopyOfBlobInContainer(containerName, blob, payload, actualHashCode); Map<String, Blob> map = containerToBlobs.get(containerName); String blobName = newBlob.getMetadata().getName(); map.put(blobName, newBlob); containerToBlobAccess.get(containerName).put(blobName, BlobAccess.PRIVATE); return base16().lowerCase().encode(actualHashCode.asBytes()); }
@Override public Model readFrom(Class<Model> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { MultivaluedMap<String, String> queryParameters = this.uriInfo.getQueryParameters(); String modelName = queryParameters.getFirst("modelName"); CountingInputStream countingIs = new CountingInputStream(entityStream); HashingInputStream hashingIs = new HashingInputStream(Hashing.md5(), countingIs); EvaluatorBuilder evaluatorBuilder; try { evaluatorBuilder = this.modelEvaluatorBuilder.clone() .load(hashingIs, modelName); } catch(SAXException | JAXBException e){ logger.error("Failed to load the PMML document", e); throw new BadRequestException(e); } catch(PMMLException pe){ logger.error("Failed to find a scorable model element", pe); throw new BadRequestException(pe); } Evaluator evaluator; try { evaluator = evaluatorBuilder.build(); evaluator.verify(); } catch(PMMLException pe){ logger.error("Failed to build a model evaluator", pe); throw new BadRequestException(pe); } Model model = new Model(evaluator); model.putProperty(Model.PROPERTY_FILE_SIZE, countingIs.getCount()); model.putProperty(Model.PROPERTY_FILE_MD5SUM, (hashingIs.hash()).toString()); return model; }
public void testHash_hashesCorrectly() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
try (HashingInputStream stream = new HashingInputStream(Hashing.sha256(), url.openStream())) { fileHash = stream.hash().toString();
private BlobId save(InputStream data, BlobId id) { String containerName = this.containerName.value(); HashingInputStream hashingInputStream = new HashingInputStream(Hashing.sha256(), data); Payload payload = payloadCodec.write(hashingInputStream); Blob blob = blobStore.blobBuilder(id.asString()).payload(payload).build(); blobStore.putBlob(containerName, blob); return blobIdFactory.from(hashingInputStream.hash().toString()); }
public void testHash_hashesCorrectlyReadOutOfBound() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(testBytes); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); byte[] buf = new byte[100]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(-1, in.read()); // additional read assertEquals(4, numOfByteRead); assertEquals(expectedHash, in.hash()); }
public void testHash_hashesCorrectlyForSkipping() throws Exception { HashCode expectedHash = Hashing.md5().hashBytes(new byte[] {'m', 's'}); HashingInputStream in = new HashingInputStream(Hashing.md5(), buffer); long numOfByteSkipped = in.skip(2); assertEquals(2, numOfByteSkipped); byte[] buf = new byte[4]; int numOfByteRead = in.read(buf, 0, buf.length); assertEquals(2, numOfByteRead); assertEquals(expectedHash, in.hash()); }