Skip to content

Commit 9bc3701

Browse files
committed
[CODEC-195] Support SHA-224 in DigestUtils on Java 8.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1637936 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1596d05 commit 9bc3701

5 files changed

Lines changed: 176 additions & 1 deletion

File tree

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,20 @@ limitations under the License.
206206
</roles>
207207
</contributor>
208208
</contributors>
209-
<!-- Codec should depend on very little -->
209+
<!-- Codec only has test dependencies ATM -->
210210
<dependencies>
211211
<dependency>
212212
<groupId>junit</groupId>
213213
<artifactId>junit</artifactId>
214214
<version>4.11</version>
215215
<scope>test</scope>
216216
</dependency>
217+
<dependency>
218+
<groupId>org.apache.commons</groupId>
219+
<artifactId>commons-lang3</artifactId>
220+
<version>3.3.2</version>
221+
<scope>test</scope>
222+
</dependency>
217223
</dependencies>
218224
<properties>
219225
<maven.compiler.source>1.6</maven.compiler.source>

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ The <action> type attribute can be add,update,fix,remove.
4343
</properties>
4444
<body>
4545
<release version="1.11" date="DD MM 2014" description="Feature and fix release.">
46+
<action dev="ggregory" type="add" issue="CODEC-195">Support SHA-224 in DigestUtils on Java 8</action>
4647
<action dev="ggregory" type="add" issue="CODEC-194">Support java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex</action>
4748
<action dev="ggregory" type="add" issue="CODEC-193">Support java.nio.ByteBuffer in DigestUtils</action>
4849
</release>

src/main/java/org/apache/commons/codec/digest/DigestUtils.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ public static MessageDigest getSha1Digest() {
129129
return getDigest(MessageDigestAlgorithms.SHA_1);
130130
}
131131

132+
/**
133+
* Returns an SHA-224 digest.
134+
* <p>
135+
* Java 8 only.
136+
* </p>
137+
*
138+
* @return An SHA-224 digest instance.
139+
* @throws IllegalArgumentException
140+
* when a {@link NoSuchAlgorithmException} is caught on Java 7 and older, SHA-224 is a built-in
141+
* algorithm on Java 8
142+
* @see MessageDigestAlgorithms#SHA_224
143+
*/
144+
public static MessageDigest getSha224Digest() {
145+
return getDigest(MessageDigestAlgorithms.SHA_224);
146+
}
147+
132148
/**
133149
* Returns an SHA-256 digest.
134150
* <p>
@@ -527,6 +543,135 @@ public static String sha1Hex(final String data) {
527543
return Hex.encodeHexString(sha1(data));
528544
}
529545

546+
/**
547+
* Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
548+
* <p>
549+
* Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
550+
* </p>
551+
*
552+
* @param data
553+
* Data to digest
554+
* @return SHA-224 digest
555+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
556+
* @since 1.11
557+
*/
558+
public static byte[] sha224(final byte[] data) {
559+
return getSha224Digest().digest(data);
560+
}
561+
562+
/**
563+
* Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
564+
*
565+
* @param data
566+
* Data to digest
567+
* @return SHA-224 digest
568+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
569+
* @since 1.11
570+
*/
571+
public static byte[] sha224(final ByteBuffer data) {
572+
return digest(getSha224Digest(), data);
573+
}
574+
575+
/**
576+
* Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
577+
* <p>
578+
* Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
579+
* </p>
580+
*
581+
* @param data
582+
* Data to digest
583+
* @return SHA-224 digest
584+
* @throws IOException
585+
* On error reading from the stream
586+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
587+
* @since 1.11
588+
*/
589+
public static byte[] sha224(final InputStream data) throws IOException {
590+
return digest(getSha224Digest(), data);
591+
}
592+
593+
/**
594+
* Calculates the SHA-224 digest and returns the value as a <code>byte[]</code>.
595+
* <p>
596+
* Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
597+
* </p>
598+
*
599+
* @param data
600+
* Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
601+
* @return SHA-224 digest
602+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
603+
* @since 1.11
604+
*/
605+
public static byte[] sha224(final String data) {
606+
return sha224(StringUtils.getBytesUtf8(data));
607+
}
608+
609+
/**
610+
* Calculates the SHA-224 digest and returns the value as a hex string.
611+
* <p>
612+
* Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
613+
* </p>
614+
*
615+
* @param data
616+
* Data to digest
617+
* @return SHA-224 digest as a hex string
618+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
619+
* @since 1.11
620+
*/
621+
public static String sha224Hex(final byte[] data) {
622+
return Hex.encodeHexString(sha224(data));
623+
}
624+
625+
/**
626+
* Calculates the SHA-224 digest and returns the value as a hex string.
627+
* <p>
628+
* Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
629+
* </p>
630+
*
631+
* @param data
632+
* Data to digest
633+
* @return SHA-224 digest as a hex string
634+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
635+
* @since 1.11
636+
*/
637+
public static String sha224Hex(final ByteBuffer data) {
638+
return Hex.encodeHexString(sha224(data));
639+
}
640+
641+
/**
642+
* Calculates the SHA-224 digest and returns the value as a hex string.
643+
* <p>
644+
* Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
645+
* </p>
646+
*
647+
* @param data
648+
* Data to digest
649+
* @return SHA-224 digest as a hex string
650+
* @throws IOException
651+
* On error reading from the stream
652+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
653+
* @since 1.11
654+
*/
655+
public static String sha224Hex(final InputStream data) throws IOException {
656+
return Hex.encodeHexString(sha224(data));
657+
}
658+
659+
/**
660+
* Calculates the SHA-224 digest and returns the value as a hex string.
661+
* <p>
662+
* Throws a {@link IllegalArgumentException} on JRE versions prior to 1.4.0.
663+
* </p>
664+
*
665+
* @param data
666+
* Data to digest
667+
* @return SHA-224 digest as a hex string
668+
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
669+
* @since 1.11
670+
*/
671+
public static String sha224Hex(final String data) {
672+
return Hex.encodeHexString(sha224(data));
673+
}
674+
530675
/**
531676
* Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
532677
* <p>

src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ private MessageDigestAlgorithms() {
5353
*/
5454
public static final String SHA_1 = "SHA-1";
5555

56+
/**
57+
* The SHA-224 hash algorithm defined in the FIPS PUB 180-4.
58+
* <p>
59+
* Java 8 only.
60+
* </p>
61+
*
62+
* @since 1.11
63+
*/
64+
public static final String SHA_224 = "SHA-224";
65+
5666
/**
5767
* The SHA-256 hash algorithm defined in the FIPS PUB 180-2.
5868
*/

src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
import org.apache.commons.codec.binary.Hex;
3232
import org.apache.commons.codec.binary.StringUtils;
33+
import org.apache.commons.lang3.JavaVersion;
34+
import org.apache.commons.lang3.SystemUtils;
35+
import org.junit.Assume;
3336
import org.junit.Test;
3437

3538
/**
@@ -268,6 +271,16 @@ public void testSha1UpdateWithString(){
268271
assertEquals(expectedResult, actualResult);
269272
}
270273

274+
@Test
275+
public void testSha224() throws IOException {
276+
Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
277+
assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", DigestUtils.sha224Hex(""));
278+
assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
279+
DigestUtils.sha224Hex("The quick brown fox jumps over the lazy dog"));
280+
281+
// Examples from FIPS 180-4?
282+
}
283+
271284
@Test
272285
public void testSha256() throws IOException {
273286
// Examples from FIPS 180-2

0 commit comments

Comments
 (0)