/** Creates a new instance, which calculates a signature on the given stream, * using the given {@link MessageDigest}. * @param pStream the stream to calculate the message digest for * @param pDigest the message digest to use */ public MessageDigestCalculatingInputStream(final InputStream pStream, final MessageDigest pDigest) { super(pStream); messageDigest = pDigest; add(new MessageDigestMaintainingObserver(pDigest)); }
@Test public void test() throws Exception { for (int i = 256; i < 8192; i = i*2) { final byte[] buffer = generateRandomByteStream(i); final MessageDigest md5Sum = MessageDigest.getInstance("MD5"); final byte[] expect = md5Sum.digest(buffer); try (final MessageDigestCalculatingInputStream md5InputStream = new MessageDigestCalculatingInputStream(new ByteArrayInputStream(buffer))) { md5InputStream.consume(); final byte[] got = md5InputStream.getMessageDigest().digest(); assertArrayEquals(expect, got); } } }
/** Creates a new instance, which calculates a signature on the given stream, * using the given {@link MessageDigest}. * @param pStream the stream to calculate the message digest for * @param pDigest the message digest to use */ public MessageDigestCalculatingInputStream(final InputStream pStream, final MessageDigest pDigest) { super(pStream); messageDigest = pDigest; add(new MessageDigestMaintainingObserver(pDigest)); }