You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2016/05/23 11:12:47 UTC

svn commit: r1745140 - in /commons/proper/codec/trunk/src: main/java/org/apache/commons/codec/digest/DigestUtils.java test/java/org/apache/commons/codec/digest/DigestUtilsTest.java

Author: sebb
Date: Mon May 23 11:12:47 2016
New Revision: 1745140

URL: http://svn.apache.org/viewvc?rev=1745140&view=rev
Log:
Simplify

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=1745140&r1=1745139&r2=1745140&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 Mon May 23 11:12:47 2016
@@ -44,8 +44,8 @@ import org.apache.commons.codec.binary.S
  * <pre>
  * import static org.apache.commons.codec.digest.MessageDigestAlgorithms.SHA_224;
  * ...
- * byte [] digest = DigestUtils.with(SHA_224).update(dataToDigest).done();
- * String hdigest = DigestUtils.with(SHA_224).update(new File("pom.xml")).asHex();
+ * byte [] digest = new DigestUtils(SHA_224).digest(dataToDigest);
+ * String hdigest = new DigestUtils(SHA_224).digestAsHex(new File("pom.xml"));
  * </pre>
  * @see MessageDigestAlgorithms
  * @version $Id$
@@ -110,7 +110,7 @@ public class DigestUtils {
      * @return the digest
      * @throws IOException
      *             On error reading from the stream
-     * @since 1.11 (was private previously)
+     * @since 1.11 (was private)
      */
     public static byte[] digest(final MessageDigest messageDigest, final InputStream data) throws IOException {
         return updateDigest(messageDigest, data).digest();
@@ -959,8 +959,6 @@ public static byte[] sha(final byte[] da
         return getDigest(messageDigestAlgorithm, null) != null;
     }
 
-    // Fluent interface
-
     private final MessageDigest messageDigest;
 
     // public to maintain binary compatibility
@@ -968,30 +966,33 @@ public static byte[] sha(final byte[] da
         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 the fluent instance
+     * Creates an instance using the provided {@link MessageDigest} parameter.
+     *
+     * This can then be used to create digests using methods such as
+     * {@link #digest(byte[])} and {@link #digestAsHex(File)}.
+     *
+     * @param digest the {@link MessageDigest} to use
      * @since 1.11
      */
-    public static DigestUtils with(MessageDigest digest) {
-        return new DigestUtils(digest);
+    public DigestUtils(MessageDigest digest) {
+        this.messageDigest = digest;
     }
 
     /**
-     * Creates a {@link MessageDigest} and returns a fluent instance.
+     * Creates an instance using the provided {@link MessageDigest} parameter.
+     *
+     * This can then be used to create digests using methods such as
+     * {@link #digest(byte[])} and {@link #digestAsHex(File)}.
      *
-     * @param name the name of digest algorithm to create, e.g. {@link MessageDigestAlgorithms#MD5}
-     * @return the fluent instance
+     * @param name the name of the {@link MessageDigest} to use
+     * @see #getDigest(String)
+     * @throws IllegalArgumentException
+     *             when a {@link NoSuchAlgorithmException} is caught.
      * @since 1.11
      */
-    public static DigestUtils with(String name) {
-        return new DigestUtils(getDigest(name));
+    public DigestUtils(String name) {
+        this(getDigest(name));
     }
 
     /**
@@ -1004,107 +1005,133 @@ public static byte[] sha(final byte[] da
     }
 
     /**
-     * Completes the hash computation and returns the hash
-     * accumulated by one or more invocations of an update method.
+     * Reads through a byte array and returns the digest for the data.
      *
-     * @return the hash as a byte array
+     * @param data
+     *            Data to digest
+     * @return the digest
+     * @since 1.11
+     */
+    public byte[] digest(final byte[] data) {
+        return updateDigest(messageDigest, data).digest();
+    }
+
+    /**
+     * Reads through a byte array and returns the digest for the data.
      *
+     * @param data
+     *            Data to digest treated as UTF-8 string
+     * @return the digest
      * @since 1.11
      */
-    public byte[] done() {
-        return messageDigest.digest();
+    public byte[] digest(final String data) {
+        return updateDigest(messageDigest, data).digest();
     }
 
     /**
-     * Completes the hash computation and returns the hash
-     * accumulated by one or more invocations of an update method.
+     * Reads through a ByteBuffer and returns the digest for the data
      *
-     * @return the hash as a hex String
+     * @param data
+     *            Data to digest
+     * @return the digest
      *
      * @since 1.11
      */
-    public String asHex() {
-        return Hex.encodeHexString(messageDigest.digest());
+    public byte[] digest(final ByteBuffer data) {
+        return updateDigest(messageDigest, data).digest();
     }
 
     /**
-     * Updates the {@link MessageDigest} in the {@link DigestUtils} instance
+     * Reads through a File and returns the digest for the data
      *
      * @param data
-     *            the data to update the {@link MessageDigest} with
-     * @return the updated {@link DigestUtils}
+     *            Data to digest
+     * @return the digest
+     * @throws IOException
+     *             On error reading from the stream
      * @since 1.11
      */
-    public DigestUtils update(final byte[] data) {
-        messageDigest.update(data);
-        return this;
+    public byte[] digest(final File data) throws IOException {
+        return updateDigest(messageDigest, data).digest();
+    }
+
+    /**
+     * Reads through an InputStream and returns the digest for the data
+     *
+     * @param data
+     *            Data to digest
+     * @return the digest
+     * @throws IOException
+     *             On error reading from the stream
+     * @since 1.11
+     */
+    public byte[] digest(final InputStream data) throws IOException {
+        return updateDigest(messageDigest, data).digest();
     }
 
     /**
-     * Updates the {@link MessageDigest} in the {@link DigestUtils} instance
+     * Reads through a byte array and returns the digest for the data.
      *
      * @param data
-     *            the data to update the {@link MessageDigest} with
-     * @return the updated {@link DigestUtils}
+     *            Data to digest
+     * @return the digest as a hex string
      * @since 1.11
      */
-    public DigestUtils update(final ByteBuffer data) {
-        messageDigest.update(data);
-        return this;
+    public String digestAsHex(final byte[] data) {
+        return Hex.encodeHexString(digest(data));
     }
 
     /**
-     * Updates the {@link MessageDigest} in the {@link DigestUtils} instance
+     * Reads through a byte array and returns the digest for the data.
      *
      * @param data
-     *            the data to update the {@link MessageDigest} with
-     * @return the updated {@link DigestUtils}
+     *            Data to digest treated as UTF-8 string
+     * @return the digest as a hex string
      * @since 1.11
      */
-    public DigestUtils update(final String data) {
-        messageDigest.update(StringUtils.getBytesUtf8(data));
-        return this;
+    public String digestAsHex(final String data) {
+        return Hex.encodeHexString(digest(data));
     }
 
     /**
-     * Updates the {@link MessageDigest} in the {@link DigestUtils} instance
+     * Reads through a ByteBuffer and returns the digest for the data
      *
      * @param data
-     *            the data to update the {@link MessageDigest} with
-     * @return the updated {@link DigestUtils}
-     * @throws IOException
-     *             If some I/O error occurs.
+     *            Data to digest
+     * @return the digest as a hex string
+     *
      * @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);
+    public String digestAsHex(final ByteBuffer data) {
+        return Hex.encodeHexString(digest(data));
+    }
 
-        while (read > -1) {
-            messageDigest.update(buffer, 0, read);
-            read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
-        }
-        return this;
+    /**
+     * Reads through a File and returns the digest for the data
+     *
+     * @param data
+     *            Data to digest
+     * @return the digest as a hex string
+     * @throws IOException
+     *             On error reading from the stream
+     * @since 1.11
+     */
+    public String digestAsHex(final File data) throws IOException {
+        return Hex.encodeHexString(digest(data));
     }
 
     /**
-     * Updates the {@link MessageDigest} in the {@link DigestUtils} instance
+     * Reads through an InputStream and returns the digest for the data
      *
      * @param data
-     *            the data to update the {@link MessageDigest} with
-     * @return the updated {@link DigestUtils}
+     *            Data to digest
+     * @return the digest as a hex string
      * @throws IOException
-     *             If some I/O error occurs.
-     * @throws SecurityException
-     *             if a security manager exists and its {@code checkRead} method denies read access to the file.
+     *             On error reading from the stream
      * @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();
-        }
+    public String digestAsHex(final InputStream data) throws IOException {
+        return Hex.encodeHexString(digest(data));
     }
+
 }

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=1745140&r1=1745139&r2=1745140&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 Mon May 23 11:12:47 2016
@@ -259,9 +259,9 @@ public class DigestUtilsTest {
     public void testSha224() throws IOException {
         assumeJava8();
         assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
-                DigestUtils.with(MessageDigestAlgorithms.SHA_224).update(("")).asHex());
+                new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex(("")));
         assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
-                DigestUtils.with(MessageDigestAlgorithms.SHA_224).update("The quick brown fox jumps over the lazy dog").asHex());
+                new DigestUtils(MessageDigestAlgorithms.SHA_224).digestAsHex("The quick brown fox jumps over the lazy dog"));
 
         // Examples from FIPS 180-4?
     }