/** * Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the * supplied name) as the returned stream is used. This can be used to compute the hash of a stream while the stream is being * processed by another reader, and saves from having to process the same stream twice. * * @param digestName the name of the hashing function (or {@link MessageDigest message digest}) that should be used * @param inputStream the stream containing the content that is to be hashed * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingInputStream createHashingStream( String digestName, InputStream inputStream ) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance(digestName); return new HashingInputStream(digest, inputStream); }
IoUtil.write(hashingStream, new BufferedOutputStream(new FileOutputStream(tmpFile)), AbstractBinaryStore.MEDIUM_BUFFER_SIZE); byte[] sha1 = hashingStream.getHash(); BinaryKey key = new BinaryKey(sha1);
IoUtil.write(hashingStream, new BufferedOutputStream(new FileOutputStream(tmpFile)), AbstractBinaryStore.MEDIUM_BUFFER_SIZE); byte[] sha1 = hashingStream.getHash(); BinaryKey key = new BinaryKey(sha1);
protected void assertSecureHashStreamWorks( Algorithm algorithm, String resourceName ) throws IOException, NoSuchAlgorithmException { // Find the content of the file ... InputStream stream = getClass().getResourceAsStream(resourceName); assertThat(stream, is(notNullValue())); byte[] bytesThruStream = IoUtil.readBytes(stream); // Find the secure hash of the file ... stream = getClass().getResourceAsStream(resourceName); assertThat(stream, is(notNullValue())); byte[] hashThruStream = null; try { hashThruStream = SecureHash.getHash(algorithm, stream); } finally { stream.close(); } // Now try reading the stream using a hash stream ... stream = getClass().getResourceAsStream(resourceName); assertThat(stream, is(notNullValue())); HashingInputStream hashingStream = SecureHash.createHashingStream(algorithm, stream); byte[] bytesThruHashingStream = IoUtil.readBytes(hashingStream); // closes stream byte[] hashThruHashingStream = hashingStream.getHash(); // The content should be the same .. assertThat(bytesThruHashingStream, is(bytesThruStream)); // The hash should also be the same ... assertThat(hashThruHashingStream, is(hashThruStream)); // System.out.println(algorithm.digestName() + "---> " + hashingStream.getHashAsHexString() + " of " + resourceName); }
/** * Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the * supplied name) as the returned stream is used. This can be used to compute the hash of a stream while the stream is being * processed by another reader, and saves from having to process the same stream twice. * * @param digestName the name of the hashing function (or {@link MessageDigest message digest}) that should be used * @param inputStream the stream containing the content that is to be hashed * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingInputStream createHashingStream( String digestName, InputStream inputStream ) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance(digestName); return new HashingInputStream(digest, inputStream); }
/** * Create an InputStream instance that wraps another stream and that computes the secure hash (using the algorithm with the * supplied name) as the returned stream is used. This can be used to compute the hash of a stream while the stream is being * processed by another reader, and saves from having to process the same stream twice. * * @param digestName the name of the hashing function (or {@link MessageDigest message digest}) that should be used * @param inputStream the stream containing the content that is to be hashed * @return the hash of the contents as a byte array * @throws NoSuchAlgorithmException */ public static HashingInputStream createHashingStream( String digestName, InputStream inputStream ) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance(digestName); return new HashingInputStream(digest, inputStream); }