commons-commits mailing list archives

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

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

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=1380022&r1=1380021&r2=1380022&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:32:04 2012
@@ -399,6 +399,55 @@ public class DigestUtils {
     }
 
     /**
+     * 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[] sha1(String data) {
+        return sha1(getBytesUtf8(data));
+    }
+
+    /**
+     * Calculates the SHA-1 digest and returns the value as a hex string.
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-1 digest as a hex string
+     * @since 1.7
+     */
+    public static String sha1Hex(byte[] data) {
+        return Hex.encodeHexString(sha1(data));
+    }
+
+    /**
+     * Calculates the SHA-1 digest and returns the value as a hex string.
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-1 digest as a hex string
+     * @throws IOException
+     *             On error reading from the stream
+     * @since 1.7
+     */
+    public static String sha1Hex(InputStream data) throws IOException {
+        return Hex.encodeHexString(sha1(data));
+    }
+
+    /**
+     * Calculates the SHA-1 digest and returns the value as a hex string.
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-1 digest as a hex string
+     * @since 1.7
+     */
+    public static String sha1Hex(String data) {
+        return Hex.encodeHexString(sha1(data));
+    }
+
+    /**
      * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
      * <p>
      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.

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=1380022&r1=1380021&r2=1380022&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:32:04 2012
@@ -234,6 +234,20 @@ public class DigestUtilsTest {
     }
 
     @Test
+    public void testSha1Hex() throws IOException {
+        // Examples from FIPS 180-1
+        assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex("abc"));
+
+        assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.sha1Hex(getBytesUtf8("abc")));
+
+        assertEquals(
+            "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
+            DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
+        assertEquals(DigestUtils.shaHex(testData),
+                DigestUtils.shaHex(new ByteArrayInputStream(testData)));
+    }
+
+    @Test
     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!'";



Mime
View raw message