commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1380018 - 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:27:02 GMT
Author: ggregory
Date: Sun Sep  2 18:27:02 2012
New Revision: 1380018

URL: http://svn.apache.org/viewvc?rev=1380018&view=rev
Log:
[CODEC-157] DigestUtils: Add MD2 APIs.

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=1380018&r1=1380017&r2=1380018&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Sun Sep  2 18:27:02 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-157" dev="ggregory" type="add" due-to="ggregory">
+        DigestUtils: Add MD2 APIs.
+      </action>
       <action issue="CODEC-156" dev="ggregory" type="add" due-to="ggregory">
         DigestUtils: add APIs named after standard alg name SHA-1.
       </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=1380018&r1=1380017&r2=1380018&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:27:02 2012
@@ -90,6 +90,19 @@ public class DigestUtils {
     }
 
     /**
+     * Returns an MD2 MessageDigest.
+     *
+     * @return An MD2 digest instance.
+     * @throws IllegalArgumentException
+     *             when a {@link NoSuchAlgorithmException} is caught, which should never
happen because MD2 is a
+     *             built-in algorithm
+     * @see MessageDigestAlgorithms#MD2
+     */
+    public static MessageDigest getMd2Digest() {
+        return getDigest(MessageDigestAlgorithms.MD2);
+    }
+
+    /**
      * Returns an MD5 MessageDigest.
      *
      * @return An MD5 digest instance.
@@ -176,6 +189,82 @@ public class DigestUtils {
     }
 
     /**
+     * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>.
+     *
+     * @param data
+     *            Data to digest
+     * @return MD2 digest
+     * @since 1.7
+     */
+    public static byte[] md2(byte[] data) {
+        return getMd2Digest().digest(data);
+    }
+
+    /**
+     * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>.
+     *
+     * @param data
+     *            Data to digest
+     * @return MD2 digest
+     * @throws IOException
+     *             On error reading from the stream
+     * @since 1.7
+     */
+    public static byte[] md2(InputStream data) throws IOException {
+        return digest(getMd2Digest(), data);
+    }
+
+    /**
+     * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>.
+     *
+     * @param data
+     *            Data to digest
+     * @return MD2 digest
+     * @since 1.7
+     */
+    public static byte[] md2(String data) {
+        return md2(getBytesUtf8(data));
+    }
+
+    /**
+     * Calculates the MD2 digest and returns the value as a 32 character hex string.
+     *
+     * @param data
+     *            Data to digest
+     * @return MD2 digest as a hex string
+     * @since 1.7
+     */
+    public static String md2Hex(byte[] data) {
+        return Hex.encodeHexString(md2(data));
+    }
+
+    /**
+     * Calculates the MD2 digest and returns the value as a 32 character hex string.
+     *
+     * @param data
+     *            Data to digest
+     * @return MD2 digest as a hex string
+     * @throws IOException
+     *             On error reading from the stream
+     * @since 1.7
+     */
+    public static String md2Hex(InputStream data) throws IOException {
+        return Hex.encodeHexString(md2(data));
+    }
+
+    /**
+     * Calculates the MD2 digest and returns the value as a 32 character hex string.
+     *
+     * @param data
+     *            Data to digest
+     * @return MD2 digest as a hex string
+     * @since 1.7
+     */
+    public static String md2Hex(String data) {
+        return Hex.encodeHexString(md2(data));
+    }
+
+    /**
      * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
      *
      * @param data
@@ -264,10 +353,12 @@ public class DigestUtils {
      * @param data
      *            Data to digest
      * @return SHA-1 digest
-     * @since 1.7
+     * @throws IOException
+     *             On error reading from the stream
+     * @since 1.4
      */
-    public static byte[] sha1(byte[] data) {
-        return getSha1Digest().digest(data);
+    public static byte[] sha(InputStream data) throws IOException {
+        return digest(getShaDigest(), data);
     }
 
     /**
@@ -276,12 +367,9 @@ public class DigestUtils {
      * @param data
      *            Data to digest
      * @return SHA-1 digest
-     * @throws IOException
-     *             On error reading from the stream
-     * @since 1.4
      */
-    public static byte[] sha(InputStream data) throws IOException {
-        return digest(getShaDigest(), data);
+    public static byte[] sha(String data) {
+        return sha(getBytesUtf8(data));
     }
 
     /**
@@ -290,12 +378,10 @@ 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);
+    public static byte[] sha1(byte[] data) {
+        return getSha1Digest().digest(data);
     }
 
     /**
@@ -304,9 +390,12 @@ 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[] sha(String data) {
-        return sha(getBytesUtf8(data));
+    public static byte[] sha1(InputStream data) throws IOException {
+        return digest(getSha1Digest(), 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=1380018&r1=1380017&r2=1380018&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:27:02 2012
@@ -66,6 +66,31 @@ public class DigestUtilsTest {
     }
 
     @Test
+    public void testMd2Hex() throws IOException {
+        // Examples from RFC 1319
+        assertEquals("8350e5a3e24c153df2275c9f80692773", DigestUtils.md2Hex(""));
+
+        assertEquals("32ec01ec4a6dac72c0ab96fb34c0b5d1", DigestUtils.md2Hex("a"));
+
+        assertEquals("da853b0d3f88d99b30283a69e6ded6bb", DigestUtils.md2Hex("abc"));
+
+        assertEquals("ab4f496bfb2a530b219ff33031fe06b0", DigestUtils.md2Hex("message digest"));
+
+        assertEquals("4e8ddff3650292ab5a4108c3aa47940b", DigestUtils.md2Hex("abcdefghijklmnopqrstuvwxyz"));
+
+        assertEquals(
+            "da33def2a42df13975352846c30338cd",
+            DigestUtils.md2Hex("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"
+ "0123456789"));
+
+        assertEquals(
+            "d5976f79d83d3a0dc9806c3c66f3efd8",
+            DigestUtils.md2Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890"));
+
+        assertEquals(DigestUtils.md2Hex(testData),
+                DigestUtils.md2Hex(new ByteArrayInputStream(testData)));
+    }
+
+    @Test
     public void testMd5Hex() throws IOException {
         // Examples from RFC 1321
         assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex(""));
@@ -91,6 +116,20 @@ public class DigestUtilsTest {
     }
 
     /**
+     * An MD2 hash converted to hex should always be 32 characters.
+     */
+    @Test
+    public void testMd2HexLength() {
+        String hashMe = "this is some string that is longer than 32 characters";
+        String hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
+        assertEquals(32, hash.length());
+
+        hashMe = "length < 32";
+        hash = DigestUtils.md2Hex(getBytesUtf8(hashMe));
+        assertEquals(32, hash.length());
+    }
+
+    /**
      * An MD5 hash converted to hex should always be 32 characters.
      */
     @Test
@@ -105,6 +144,20 @@ public class DigestUtilsTest {
     }
 
     /**
+     * An MD2 hash should always be a 16 element byte[].
+     */
+    @Test
+    public void testMd2Length() {
+        String hashMe = "this is some string that is longer than 16 characters";
+        byte[] hash = DigestUtils.md2(getBytesUtf8(hashMe));
+        assertEquals(16, hash.length);
+
+        hashMe = "length < 16";
+        hash = DigestUtils.md2(getBytesUtf8(hashMe));
+        assertEquals(16, hash.length);
+    }
+
+    /**
      * An MD5 hash should always be a 16 element byte[].
      */
     @Test



Mime
View raw message