commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1328553 - 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 Sat, 21 Apr 2012 01:16:20 GMT
Author: ggregory
Date: Sat Apr 21 01:16:19 2012
New Revision: 1328553

URL: http://svn.apache.org/viewvc?rev=1328553&view=rev
Log:
[CODEC-139] DigestUtils: add updateDigest methods and make methods public.

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=1328553&r1=1328552&r2=1328553&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Sat Apr 21 01:16:19 2012
@@ -51,7 +51,10 @@ The <action> type attribute can be add,u
     </release>
     -->
     <release version="1.7" date="TBD" description="Feature and fix release.">
-      <action dev="ggregory" type="fix" issue="CODEC-133" due-to="lathspell">
+      <action dev="ggregory" type="add" issue="CODEC-139" due-to="dsebastien">
+        DigestUtils: add updateDigest methods and make methods public.
+      </action>
+      <action dev="ggregory" type="add" issue="CODEC-133" due-to="lathspell">
         Add classes for MD5/SHA1/SHA-512-based Unix crypt(3) hash variants.
       </action>
       <action dev="ggregory" type="fix" issue="CODEC-96" due-to="sebb">

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=1328553&r1=1328552&r2=1328553&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
Sat Apr 21 01:16:19 2012
@@ -81,7 +81,7 @@ public class DigestUtils {
      * @throws RuntimeException
      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
      */
-    static MessageDigest getDigest(String algorithm) {
+    public static MessageDigest getDigest(String algorithm) {
         try {
             return MessageDigest.getInstance(algorithm);
         } catch (NoSuchAlgorithmException e) {
@@ -96,7 +96,7 @@ public class DigestUtils {
      * @throws RuntimeException
      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
      */
-    private static MessageDigest getMd5Digest() {
+    public static MessageDigest getMd5Digest() {
         return getDigest("MD5");
     }
 
@@ -110,7 +110,7 @@ public class DigestUtils {
      * @throws RuntimeException
      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
      */
-    private static MessageDigest getSha256Digest() {
+    public static MessageDigest getSha256Digest() {
         return getDigest("SHA-256");
     }
 
@@ -124,7 +124,7 @@ public class DigestUtils {
      * @throws RuntimeException
      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
      */
-    private static MessageDigest getSha384Digest() {
+    public static MessageDigest getSha384Digest() {
         return getDigest("SHA-384");
     }
 
@@ -138,7 +138,7 @@ public class DigestUtils {
      * @throws RuntimeException
      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
      */
-    private static MessageDigest getSha512Digest() {
+    public static MessageDigest getSha512Digest() {
         return getDigest("SHA-512");
     }
 
@@ -149,7 +149,7 @@ public class DigestUtils {
      * @throws RuntimeException
      *             when a {@link java.security.NoSuchAlgorithmException} is caught.
      */
-    private static MessageDigest getShaDigest() {
+    public static MessageDigest getShaDigest() {
         return getDigest("SHA");
     }
 
@@ -578,4 +578,34 @@ public class DigestUtils {
     public static String shaHex(String data) {
         return Hex.encodeHexString(sha(data));
     }
+
+    /**
+     * Updates the given {@link MessageDigest}.
+     * 
+     * @param messageDigest
+     *            the {@link MessageDigest} to update
+     * @param valueToDigest
+     *            the value to update the {@link MessageDigest} with
+     * @return the updated {@link MessageDigest}
+     * @since 1.7
+     */
+    public static MessageDigest updateDigest(final MessageDigest messageDigest, final String
valueToDigest) {
+        messageDigest.update(getBytesUtf8(valueToDigest));
+        return messageDigest;
+    }
+
+    /**
+     * Updates the given {@link MessageDigest}.
+     * 
+     * @param messageDigest
+     *            the {@link MessageDigest} to update
+     * @param valueToDigest
+     *            the value to update the {@link MessageDigest} with
+     * @return the updated {@link MessageDigest}
+     * @since 1.7
+     */
+    public static MessageDigest updateDigest(final MessageDigest messageDigest, byte[] valueToDigest)
{
+        messageDigest.update(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=1328553&r1=1328552&r2=1328553&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
Sat Apr 21 01:16:19 2012
@@ -22,8 +22,10 @@ import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.security.MessageDigest;
 import java.util.Random;
 
+import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.codec.binary.StringUtils;
 import org.junit.Test;
 
@@ -177,4 +179,41 @@ public class DigestUtilsTest {
                 DigestUtils.shaHex(new ByteArrayInputStream(testData)));
 
     }
+    
+    @Test
+    public void testUpdateWithByteArray(){
+        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.getShaDigest();
+        messageDigest.update(d1.getBytes());
+        messageDigest.update(d2.getBytes());
+        final String expectedResult = Hex.encodeHexString(messageDigest.digest());
+
+        messageDigest = DigestUtils.getShaDigest();
+        DigestUtils.updateDigest(messageDigest, d1.getBytes());
+        DigestUtils.updateDigest(messageDigest, d2.getBytes());
+        final String actualResult = Hex.encodeHexString(messageDigest.digest());
+
+        assertEquals(expectedResult, actualResult);
+    }
+
+    @Test
+    public void testUpdateWithString(){
+        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.getShaDigest();
+        messageDigest.update(StringUtils.getBytesUtf8(d1));
+        messageDigest.update(StringUtils.getBytesUtf8(d2));
+        final String expectedResult = Hex.encodeHexString(messageDigest.digest());
+
+        messageDigest = DigestUtils.getShaDigest();
+        DigestUtils.updateDigest(messageDigest, d1);
+        DigestUtils.updateDigest(messageDigest, d2);
+        final String actualResult = Hex.encodeHexString(messageDigest.digest());
+
+        assertEquals(expectedResult, actualResult);
+    }
+
 }



Mime
View raw message