commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1637936 - in /commons/proper/codec/trunk: ./ src/changes/ src/main/java/org/apache/commons/codec/digest/ src/test/java/org/apache/commons/codec/digest/
Date Mon, 10 Nov 2014 16:47:29 GMT
Author: ggregory
Date: Mon Nov 10 16:47:29 2014
New Revision: 1637936

URL: http://svn.apache.org/r1637936
Log:
[CODEC-195] Support SHA-224 in DigestUtils on Java 8.

Modified:
    commons/proper/codec/trunk/pom.xml
    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/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java
    commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java

Modified: commons/proper/codec/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/pom.xml?rev=1637936&r1=1637935&r2=1637936&view=diff
==============================================================================
--- commons/proper/codec/trunk/pom.xml (original)
+++ commons/proper/codec/trunk/pom.xml Mon Nov 10 16:47:29 2014
@@ -206,7 +206,7 @@ limitations under the License.
       </roles>
     </contributor>
   </contributors>
-  <!-- Codec should depend on very little -->
+  <!-- Codec only has test dependencies ATM -->
   <dependencies>
     <dependency>
       <groupId>junit</groupId>
@@ -214,6 +214,12 @@ limitations under the License.
       <version>4.11</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.3.2</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <properties>
     <maven.compiler.source>1.6</maven.compiler.source>

Modified: commons/proper/codec/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/changes/changes.xml?rev=1637936&r1=1637935&r2=1637936&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Mon Nov 10 16:47:29 2014
@@ -43,6 +43,7 @@ The <action> type attribute can be add,u
   </properties>
   <body>
     <release version="1.11" date="DD MM 2014" description="Feature and fix release.">
+      <action dev="ggregory" type="add" issue="CODEC-195">Support SHA-224 in DigestUtils
on Java 8</action>         
       <action dev="ggregory" type="add" issue="CODEC-194">Support java.nio.ByteBuffer
in org.apache.commons.codec.binary.Hex</action>         
       <action dev="ggregory" type="add" issue="CODEC-193">Support java.nio.ByteBuffer
in DigestUtils</action>         
     </release>   

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=1637936&r1=1637935&r2=1637936&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 Nov 10 16:47:29 2014
@@ -130,6 +130,22 @@ public class DigestUtils {
     }
 
     /**
+     * Returns an SHA-224 digest.
+     * <p>
+     * Java 8 only.
+     * </p>
+     *
+     * @return An SHA-224 digest instance.
+     * @throws IllegalArgumentException
+     *             when a {@link NoSuchAlgorithmException} is caught on Java 7 and older,
SHA-224 is a built-in
+     *             algorithm on Java 8
+     * @see MessageDigestAlgorithms#SHA_224
+     */
+    public static MessageDigest getSha224Digest() {
+        return getDigest(MessageDigestAlgorithms.SHA_224);
+    }
+
+    /**
      * Returns an SHA-256 digest.
      * <p>
      * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
@@ -528,6 +544,135 @@ public static byte[] sha(final InputStre
     }
 
     /**
+     * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
+     * <p>
+     * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
+     * </p>
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-224 digest
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static byte[] sha224(final byte[] data) {
+        return getSha224Digest().digest(data);
+    }
+
+    /**
+     * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-224 digest
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static byte[] sha224(final ByteBuffer data) {
+        return digest(getSha224Digest(), data);
+    }
+
+    /**
+     * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
+     * <p>
+     * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
+     * </p>
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-224 digest
+     * @throws IOException
+     *             On error reading from the stream
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static byte[] sha224(final InputStream data) throws IOException {
+        return digest(getSha224Digest(), data);
+    }
+
+    /**
+     * Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
+     * <p>
+     * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
+     * </p>
+     *
+     * @param data
+     *            Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
+     * @return SHA-224 digest
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static byte[] sha224(final String data) {
+        return sha224(StringUtils.getBytesUtf8(data));
+    }
+
+    /**
+     * Calculates the SHA-224 digest and returns the value as a hex string.
+     * <p>
+     * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
+     * </p>
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-224 digest as a hex string
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static String sha224Hex(final byte[] data) {
+        return Hex.encodeHexString(sha224(data));
+    }
+
+    /**
+     * Calculates the SHA-224 digest and returns the value as a hex string.
+     * <p>
+     * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
+     * </p>
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-224 digest as a hex string
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static String sha224Hex(final ByteBuffer data) {
+        return Hex.encodeHexString(sha224(data));
+    }
+
+    /**
+     * Calculates the SHA-224 digest and returns the value as a hex string.
+     * <p>
+     * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
+     * </p>
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-224 digest as a hex string
+     * @throws IOException
+     *             On error reading from the stream
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static String sha224Hex(final InputStream data) throws IOException {
+        return Hex.encodeHexString(sha224(data));
+    }
+
+    /**
+     * Calculates the SHA-224 digest and returns the value as a hex string.
+     * <p>
+     * Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
+     * </p>
+     *
+     * @param data
+     *            Data to digest
+     * @return SHA-224 digest as a hex string
+     * @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
+     * @since 1.11
+     */
+    public static String sha224Hex(final String data) {
+        return Hex.encodeHexString(sha224(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/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java?rev=1637936&r1=1637935&r2=1637936&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java
(original)
+++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java
Mon Nov 10 16:47:29 2014
@@ -54,6 +54,16 @@ public class MessageDigestAlgorithms {
     public static final String SHA_1 = "SHA-1";
 
     /**
+     * The SHA-224 hash algorithm defined in the FIPS PUB 180-4. 
+     * <p>
+     * Java 8 only.
+     * </p>
+     * 
+     * @since 1.11
+     */
+    public static final String SHA_224 = "SHA-224";
+
+    /**
      * The SHA-256 hash algorithm defined in the FIPS PUB 180-2.
      */
     public static final String SHA_256 = "SHA-256";

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=1637936&r1=1637935&r2=1637936&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 Nov 10 16:47:29 2014
@@ -30,6 +30,9 @@ import java.util.Random;
 
 import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.codec.binary.StringUtils;
+import org.apache.commons.lang3.JavaVersion;
+import org.apache.commons.lang3.SystemUtils;
+import org.junit.Assume;
 import org.junit.Test;
 
 /**
@@ -269,6 +272,16 @@ public class DigestUtilsTest {
     }
 
     @Test
+    public void testSha224() throws IOException {
+        Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
+        assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", DigestUtils.sha224Hex(""));
+        assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
+                DigestUtils.sha224Hex("The quick brown fox jumps over the lazy dog"));
+
+        // Examples from FIPS 180-4?
+    }
+
+    @Test
     public void testSha256() throws IOException {
     // Examples from FIPS 180-2
     assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",



Mime
View raw message