commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1380014 - in /commons/proper/codec/trunk/src: changes/changes.xml main/java/org/apache/commons/codec/digest/DigestUtils.java test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
Date Sun, 02 Sep 2012 18:08:45 GMT
Author: ggregory
Date: Sun Sep  2 18:08:45 2012
New Revision: 1380014

URL: http://svn.apache.org/viewvc?rev=1380014&view=rev
Log:
[CODEC-156] DigestUtils: add APIs named after standard alg name SHA-1.

Modified:
    commons/proper/codec/trunk/src/changes/changes.xml
    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/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/changes/changes.xml?rev=1380014&r1=1380013&r2=1380014&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Sun Sep  2 18:08:45 2012
@@ -51,6 +51,9 @@ The <action> type attribute can be add,u
     </release>
     -->
     <release version="1.7" date="TBD" description="Feature and fix release.">
+      <action issue="CODEC-156" dev="ggregory" type="add" due-to="ggregory">
+        DigestUtils: add APIs named after standard alg name SHA-1.
+      </action>
       <action issue="CODEC-155" dev="ggregory" type="add" due-to="ggregory">
         DigestUtils.getDigest(String) should throw IllegalArgumentException instead of RuntimeException.
       </action>

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=1380014&r1=1380013&r2=1380014&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
Sun Sep  2 18:08:45 2012
@@ -103,6 +103,20 @@ public class DigestUtils {
     }
 
     /**
+     * Returns an SHA-1 digest.
+     *
+     * @return An SHA-1 digest instance.
+     * @throws IllegalArgumentException
+     *             when a {@link NoSuchAlgorithmException} is caught, which should never
happen because SHA-1 is a
+     *             built-in algorithm
+     * @see MessageDigestAlgorithms#SHA_1
+     * @since 1.7
+     */
+    public static MessageDigest getSha1Digest() {
+        return getDigest(MessageDigestAlgorithms.SHA_1);
+    }
+
+    /**
      * Returns an SHA-256 digest.
      * <p>
      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
@@ -250,6 +264,18 @@ public class DigestUtils {
      * @param data
      *            Data to digest
      * @return SHA-1 digest
+     * @since 1.7
+     */
+    public static byte[] sha1(byte[] data) {
+        return getSha1Digest().digest(data);
+    }
+
+    /**
+     * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-1 digest
      * @throws IOException
      *             On error reading from the stream
      * @since 1.4
@@ -264,6 +290,20 @@ public class DigestUtils {
      * @param data
      *            Data to digest
      * @return SHA-1 digest
+     * @throws IOException
+     *             On error reading from the stream
+     * @since 1.7
+     */
+    public static byte[] sha1(InputStream data) throws IOException {
+        return digest(getSha1Digest(), data);
+    }
+
+    /**
+     * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-1 digest
      */
     public static byte[] sha(String data) {
         return sha(getBytesUtf8(data));
@@ -597,8 +637,8 @@ public class DigestUtils {
      * @return the updated {@link MessageDigest}
      * @since 1.7
      */
-    public static MessageDigest updateDigest(final MessageDigest messageDigest, final String
valueToDigest) {
-        messageDigest.update(getBytesUtf8(valueToDigest));
+    public static MessageDigest updateDigest(final MessageDigest messageDigest, byte[] valueToDigest)
{
+        messageDigest.update(valueToDigest);
         return messageDigest;
     }
 
@@ -612,8 +652,8 @@ public class DigestUtils {
      * @return the updated {@link MessageDigest}
      * @since 1.7
      */
-    public static MessageDigest updateDigest(final MessageDigest messageDigest, byte[] valueToDigest)
{
-        messageDigest.update(valueToDigest);
+    public static MessageDigest updateDigest(final MessageDigest messageDigest, final String
valueToDigest) {
+        messageDigest.update(getBytesUtf8(valueToDigest));
         return messageDigest;
     }
 }

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=1380014&r1=1380013&r2=1380014&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
Sun Sep  2 18:08:45 2012
@@ -182,7 +182,25 @@ public class DigestUtilsTest {
     }
 
     @Test
-    public void testUpdateWithByteArray(){
+    public void testSha1UpdateWithByteArray(){
+        final String d1 = "C'est un homme qui rentre dans un café, et plouf";
+        final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau:
'oh... encore un chapeau!'";
+
+        MessageDigest messageDigest = DigestUtils.getSha1Digest();
+        messageDigest.update(d1.getBytes());
+        messageDigest.update(d2.getBytes());
+        final String expectedResult = Hex.encodeHexString(messageDigest.digest());
+
+        messageDigest = DigestUtils.getSha1Digest();
+        DigestUtils.updateDigest(messageDigest, d1.getBytes());
+        DigestUtils.updateDigest(messageDigest, d2.getBytes());
+        final String actualResult = Hex.encodeHexString(messageDigest.digest());
+
+        assertEquals(expectedResult, actualResult);
+    }
+
+    @Test
+    public void testShaUpdateWithByteArray(){
         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau:
'oh... encore un chapeau!'";
 
@@ -200,7 +218,25 @@ public class DigestUtilsTest {
     }
 
     @Test
-    public void testUpdateWithString(){
+    public void testSha1UpdateWithString(){
+        final String d1 = "C'est un homme qui rentre dans un café, et plouf";
+        final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau:
'oh... encore un chapeau!'";
+
+        MessageDigest messageDigest = DigestUtils.getSha1Digest();
+        messageDigest.update(StringUtils.getBytesUtf8(d1));
+        messageDigest.update(StringUtils.getBytesUtf8(d2));
+        final String expectedResult = Hex.encodeHexString(messageDigest.digest());
+
+        messageDigest = DigestUtils.getSha1Digest();
+        DigestUtils.updateDigest(messageDigest, d1);
+        DigestUtils.updateDigest(messageDigest, d2);
+        final String actualResult = Hex.encodeHexString(messageDigest.digest());
+
+        assertEquals(expectedResult, actualResult);
+    }
+
+    @Test
+    public void testShaUpdateWithString(){
         final String d1 = "C'est un homme qui rentre dans un café, et plouf";
         final String d2 = "C'est un homme, c'est qu'une tête, on lui offre un cadeau:
'oh... encore un chapeau!'";
 



Mime
View raw message