Refactor for reuse.
MODIFY
src/main/java/org/apache/commons/io/input/MessageDigestCalculatingInputStream.java
src/main/java/org/apache/commons/io/input/MessageDigestCalculatingInputStream.java
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import java.security.Provider;
/**
*/
public class MessageDigestCalculatingInputStream extends ObservableInputStream {
+ /**
+ * The default message digest algorithm.
+ */
+ private static final String DEFAULT_ALGORITHM = "MD5";
+
/**
* Maintains the message digest.
*/
@@ -61,13 +67,16 @@ public void data(final int input) throws IOException {
/**
* Creates a new instance, which calculates a signature on the given stream, using a {@link MessageDigest} with the
* "MD5" algorithm.
+ * <p>
+ * The MD5 algorithm is weak and should not be used.
+ * </p>
*
* @param inputStream the stream to calculate the message digest for
* @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation for the specified
* algorithm.
*/
public MessageDigestCalculatingInputStream(final InputStream inputStream) throws NoSuchAlgorithmException {
- this(inputStream, MessageDigest.getInstance("MD5"));
+ this(inputStream, getDefaultMessageDigest());
}
/** Creates a new instance, which calculates a signature on the given stream,
@@ -94,6 +103,19 @@ public MessageDigestCalculatingInputStream(final InputStream inputStream, final
this(inputStream, MessageDigest.getInstance(algorithm));
}
+ /**
+ * Gets a MessageDigest object that implements the a digest algorithm.
+ *
+ * @return a Message Digest object that implements the specified algorithm.
+ *
+ * @throws NoSuchAlgorithmException if no Provider supports a MessageDigestSpi implementation.
+ *
+ * @see Provider
+ */
+ static MessageDigest getDefaultMessageDigest() throws NoSuchAlgorithmException {
+ return MessageDigest.getInstance(DEFAULT_ALGORITHM);
+ }
+
/** Returns the {@link MessageDigest}, which is being used for generating the
* checksum.
* <em>Note</em>: The checksum will only reflect the data, which has been read so far.

MODIFY
src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
import org.junit.jupiter.api.Test;
+/**
+ * Tests {@link MessageDigestCalculatingInputStream}.
+ */
public class MessageDigestCalculatingInputStreamTest {
+
public static byte[] generateRandomByteStream(final int pSize) {
final byte[] buffer = new byte[pSize];
final Random rnd = new Random();
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);
+ final MessageDigest messageDigest = MessageDigestCalculatingInputStream.getDefaultMessageDigest();
+ final byte[] expect = messageDigest.digest(buffer);
+ try (final MessageDigestCalculatingInputStream messageDigestInputStream = new MessageDigestCalculatingInputStream(
+ new ByteArrayInputStream(buffer))) {
+ messageDigestInputStream.consume();
+ assertArrayEquals(expect, messageDigestInputStream.getMessageDigest().digest());
}
}
}

Refactor for reuse.