Author: sebb Date: Wed May 18 23:36:48 2016 New Revision: 1744467 URL: http://svn.apache.org/viewvc?rev=1744467&view=rev Log: CODEC-220 Fluent interface for DigestUtils
Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java?rev=1744467&r1=1744466&r2=1744467&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/DigestUtils.java Wed May 18 23:36:48 2016 @@ -45,8 +45,8 @@ import org.apache.commons.codec.binary.S * <pre> * import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224; * ... - * byte [] digest = DigestUtils.digest(SHA_224, dataToDigest); - * byte [] pommed = DigestUtils.digest(SHA_224, new File("pom.xml")); + * byte [] digest = DigestUtils.with(SHA_224).update(dataToDigest).done(); + * String hdigest = DigestUtils.with(SHA_224).update(new File("pom.xml")).asHex(); * </pre> * </code> * @see MessageDigestAlgorithms @@ -80,8 +80,7 @@ public class DigestUtils { * @param data * Data to digest * @return the digest - * @throws IOException - * On error reading from the stream + * * @since 1.11 */ public static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) { @@ -122,72 +121,6 @@ public class DigestUtils { } /** - * Reads through a byte array and returns the digest for the data. - * - * @param digestName - * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") - * @param data - * Data to digest - * @return the digest - * @throws IOException - * On error reading from the stream - * @since 1.11 - */ - public static byte[] digest(final String digestName, final byte[] data) { - return digest(getDigest(digestName), data); - } - - /** - * Reads through a ByteBuffer and returns the digest for the data - * - * @param digestName - * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") - * @param data - * Data to digest - * @return the digest - * @throws IOException - * On error reading from the stream - * @since 1.11 - */ - public static byte[] digest(final String digestName, final ByteBuffer data) { - MessageDigest messageDigest = getDigest(digestName); - messageDigest .update(data); - return messageDigest.digest(); - } - - /** - * Reads through a File and returns the digest for the data - * - * @param digestName - * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") - * @param data - * Data to digest - * @return the digest - * @throws IOException - * On error reading from the stream - * @since 1.11 - */ - public static byte[] digest(final String digestName, final File data) throws IOException { - return updateDigest(getDigest(digestName), data).digest(); - } - - /** - * Reads through an InputStream and returns the digest for the data - * - * @param digestName - * The name of the algorithm to use (e.g. MessageDigestAlgoriths.MD5 or "MD5") - * @param data - * Data to digest - * @return the digest - * @throws IOException - * On error reading from the stream - * @since 1.11 - */ - public static byte[] digest(final String digestName, final InputStream data) throws IOException { - return updateDigest(getDigest(digestName), data).digest(); - } - - /** * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>. * * @param algorithm @@ -1030,4 +963,142 @@ public static byte[] sha(final byte[] da return getDigest(messageDigestAlgorithm, null) != null; } + // Fluent interface + + private final MessageDigest messageDigest; + + DigestUtils() { + this.messageDigest = null; + } + + private DigestUtils(MessageDigest digest) { + this.messageDigest = digest; + } + + /** + * Returns a fluent instance for the digest algorithm. + * Does not reset the digest before use. + * @param digest the digest instance to use + * @return + */ + public static DigestUtils with(MessageDigest digest) { + return new DigestUtils(digest); + } + + /** + * Creates a {@link MessageDigest} and returns a fluent instance. + * + * @param name the name of digest algorithm to create, e.g. {@link MessageDigestAlgorithms#MD5} + * @return + */ + public static DigestUtils with(String name) { + return new DigestUtils(getDigest(name)); + } + + /** + * Returns the message digest instance. + * @return the message digest instance + */ + public MessageDigest getMessageDigest() { + return messageDigest; + } + + /** + * Completes the hash computation and returns the hash + * accumulated by one or more invocations of an update method. + * + * @return the hash as a byte array + * + * @since 1.11 + */ + public byte[] done() { + return messageDigest.digest(); + } + + /** + * Completes the hash computation and returns the hash + * accumulated by one or more invocations of an update method. + * + * @return the hash as a hex String + * + * @since 1.11 + */ + public String asHex() { + return Hex.encodeHexString(messageDigest.digest()); + } + + /** + * Updates the {@link MessageDigest} in the {@link DigestUtils} instance + * + * @param valueToDigest + * the value to update the {@link MessageDigest} with + * @return the updated {@link DigestUtils} + * @since 1.11 + */ + public DigestUtils update(final byte[] data) throws IOException { + messageDigest.update(data); + return this; + } + + /** + * Updates the {@link MessageDigest} in the {@link DigestUtils} instance + * + * @param valueToDigest + * the value to update the {@link MessageDigest} with + * @return the updated {@link DigestUtils} + * @since 1.11 + */ + public DigestUtils update(final ByteBuffer data) throws IOException { + messageDigest.update(data); + return this; + } + + /** + * Updates the {@link MessageDigest} in the {@link DigestUtils} instance + * + * @param valueToDigest + * the value to update the {@link MessageDigest} with + * @return the updated {@link DigestUtils} + * @since 1.11 + */ + public DigestUtils update(final String data) throws IOException { + messageDigest.update(StringUtils.getBytesUtf8(data)); + return this; + } + + /** + * Updates the {@link MessageDigest} in the {@link DigestUtils} instance + * + * @param valueToDigest + * the value to update the {@link MessageDigest} with + * @return the updated {@link DigestUtils} + * @since 1.11 + */ + public DigestUtils update(final InputStream data) throws IOException { + final byte[] buffer = new byte[STREAM_BUFFER_LENGTH]; + int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); + + while (read > -1) { + messageDigest.update(buffer, 0, read); + read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); + } + return this; + } + + /** + * Updates the {@link MessageDigest} in the {@link DigestUtils} instance + * + * @param valueToDigest + * the value to update the {@link MessageDigest} with + * @return the updated {@link DigestUtils} + * @since 1.11 + */ + public DigestUtils update(final File data) throws IOException { + final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data)); + try { + return update(stream); + } finally { + stream.close(); + } + } } Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java?rev=1744467&r1=1744466&r2=1744467&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java Wed May 18 23:36:48 2016 @@ -263,10 +263,11 @@ public class DigestUtilsTest { @Test public void testSha224() throws IOException { - assumeJava8(); assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", - Hex.encodeHexString(DigestUtils.digest(MessageDigestAlgorithms.SHA_224,StringUtils.getBytesUtf8("")))); + assumeJava8(); + assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", + DigestUtils.with(MessageDigestAlgorithms.SHA_224).update(("")).asHex()); assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525", - Hex.encodeHexString(DigestUtils.digest(MessageDigestAlgorithms.SHA_224,StringUtils.getBytesUtf8("The quick brown fox jumps over the lazy dog")))); + DigestUtils.with(MessageDigestAlgorithms.SHA_224).update("The quick brown fox jumps over the lazy dog").asHex()); // Examples from FIPS 180-4? }