You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/12/27 14:13:04 UTC
[commons-codec] branch master updated: [CODEC-274] Add SHA-512/224
and SHA-512/256 to DigestUtils for Java 9 and up.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new 01b6911 [CODEC-274] Add SHA-512/224 and SHA-512/256 to DigestUtils for Java 9 and up.
01b6911 is described below
commit 01b69110b181aa30406d18f1a689d07780c3b4a8
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Dec 27 09:13:00 2019 -0500
[CODEC-274] Add SHA-512/224 and SHA-512/256 to DigestUtils for Java 9
and up.
---
src/changes/changes.xml | 1 +
.../apache/commons/codec/digest/DigestUtils.java | 176 +++++++++++++++++++++
.../codec/digest/MessageDigestAlgorithms.java | 30 +++-
.../commons/codec/digest/DigestUtilsTest.java | 49 ++++++
4 files changed, 251 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d947376..7297c98 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -53,6 +53,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="CODEC-269" dev="aherbert" type="fix">Allow repeat calls to MurmurHash3.IncrementalHash32.end() to generate the same value.</action>
<action issue="CODEC-272" dev="ggregory" type="add" due-to="Behrang, Alex Herbert, Gary Gregory">Add RandomAccessFile digest methods #31.</action>
<action issue="CODEC-273" dev="ggregory" type="add" due-to="Gary Gregory">Add Path APIs to org.apache.commons.codec.digest.DigestUtils similar to File APIs.</action>
+ <action issue="CODEC-274" dev="ggregory" type="add" due-to="Gary Gregory">Add SHA-512/224 and SHA-512/256 to DigestUtils for Java 9 and up.</action>
</release>
<release version="1.13" date="2019-07-20" description="Feature and fix release.">
diff --git a/src/main/java/org/apache/commons/codec/digest/DigestUtils.java b/src/main/java/org/apache/commons/codec/digest/DigestUtils.java
index 631306b..17175e3 100644
--- a/src/main/java/org/apache/commons/codec/digest/DigestUtils.java
+++ b/src/main/java/org/apache/commons/codec/digest/DigestUtils.java
@@ -334,6 +334,30 @@ public class DigestUtils {
}
/**
+ * Returns an SHA-512/224 digest.
+ *
+ * @return An SHA-512/224 digest instance.
+ * @throws IllegalArgumentException
+ * when a {@link NoSuchAlgorithmException} is caught.
+ * @see MessageDigestAlgorithms#SHA_512_224
+ */
+ public static MessageDigest getSha512_224Digest() {
+ return getDigest(MessageDigestAlgorithms.SHA_512_224);
+ }
+
+ /**
+ * Returns an SHA-512/256 digest.
+ *
+ * @return An SHA-512/256 digest instance.
+ * @throws IllegalArgumentException
+ * when a {@link NoSuchAlgorithmException} is caught.
+ * @see MessageDigestAlgorithms#SHA_512_224
+ */
+ public static MessageDigest getSha512_256Digest() {
+ return getDigest(MessageDigestAlgorithms.SHA_512_256);
+ }
+
+ /**
* Returns an SHA-1 digest.
*
* @return An SHA-1 digest instance.
@@ -1090,6 +1114,30 @@ public class DigestUtils {
}
/**
+ * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/224 digest
+ * @since 1.14
+ */
+ public static byte[] sha512_224(final byte[] data) {
+ return getSha512_224Digest().digest(data);
+ }
+
+ /**
+ * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/256 digest
+ * @since 1.14
+ */
+ public static byte[] sha512_256(final byte[] data) {
+ return getSha512_256Digest().digest(data);
+ }
+
+ /**
* Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
*
* @param data
@@ -1104,6 +1152,34 @@ public class DigestUtils {
}
/**
+ * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/224 digest
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.14
+ */
+ public static byte[] sha512_224(final InputStream data) throws IOException {
+ return digest(getSha512_224Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/256 digest
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.14
+ */
+ public static byte[] sha512_256(final InputStream data) throws IOException {
+ return digest(getSha512_256Digest(), data);
+ }
+
+ /**
* Calculates the SHA-512 digest and returns the value as a {@code byte[]}.
*
* @param data
@@ -1116,6 +1192,30 @@ public class DigestUtils {
}
/**
+ * Calculates the SHA-512/224 digest and returns the value as a {@code byte[]}.
+ *
+ * @param data
+ * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
+ * @return SHA-512/224 digest
+ * @since 1.14
+ */
+ public static byte[] sha512_224(final String data) {
+ return sha512_224(StringUtils.getBytesUtf8(data));
+ }
+
+ /**
+ * Calculates the SHA-512/256 digest and returns the value as a {@code byte[]}.
+ *
+ * @param data
+ * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)}
+ * @return SHA-512/224 digest
+ * @since 1.14
+ */
+ public static byte[] sha512_256(final String data) {
+ return sha512_256(StringUtils.getBytesUtf8(data));
+ }
+
+ /**
* Calculates the SHA-512 digest and returns the value as a hex string.
*
* @param data
@@ -1128,6 +1228,58 @@ public class DigestUtils {
}
/**
+ * Calculates the SHA-512/224 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/224 digest as a hex string
+ * @since 1.14
+ */
+ public static String sha512_224Hex(final byte[] data) {
+ return Hex.encodeHexString(sha512_224(data));
+ }
+
+ /**
+ * Calculates the SHA-512/256 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/256 digest as a hex string
+ * @since 1.14
+ */
+ public static String sha512_256Hex(final byte[] data) {
+ return Hex.encodeHexString(sha512_256(data));
+ }
+
+ /**
+ * Calculates the SHA-512/224 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/224 digest as a hex string
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.14
+ */
+ public static String sha512_224Hex(final InputStream data) throws IOException {
+ return Hex.encodeHexString(sha512_224(data));
+ }
+
+ /**
+ * Calculates the SHA-512/256 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/256 digest as a hex string
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.14
+ */
+ public static String sha512_256Hex(final InputStream data) throws IOException {
+ return Hex.encodeHexString(sha512_256(data));
+ }
+
+ /**
* Calculates the SHA-512 digest and returns the value as a hex string.
*
* @param data
@@ -1154,6 +1306,30 @@ public class DigestUtils {
}
/**
+ * Calculates the SHA-512/224 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/224 digest as a hex string
+ * @since 1.14
+ */
+ public static String sha512_224Hex(final String data) {
+ return Hex.encodeHexString(sha512_224(data));
+ }
+
+ /**
+ * Calculates the SHA-512/256 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512/256 digest as a hex string
+ * @since 1.14
+ */
+ public static String sha512_256Hex(final String data) {
+ return Hex.encodeHexString(sha512_256(data));
+ }
+
+ /**
* Calculates the SHA-1 digest and returns the value as a hex string.
*
* @param data
diff --git a/src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java b/src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java
index 4469290..3a79dec 100644
--- a/src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java
+++ b/src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java
@@ -94,9 +94,29 @@ public class MessageDigestAlgorithms {
public static final String SHA_512 = "SHA-512";
/**
+ * The SHA-512 hash algorithm defined in the FIPS PUB 180-4.
+ * <p>
+ * Included starting in Oracle Java 9.
+ * </p>
+ *
+ * @since 1.14
+ */
+ public static final String SHA_512_224 = "SHA-512/224";
+
+ /**
+ * The SHA-512 hash algorithm defined in the FIPS PUB 180-4.
+ * <p>
+ * Included starting in Oracle Java 9.
+ * </p>
+ *
+ * @since 1.14
+ */
+ public static final String SHA_512_256 = "SHA-512/256";
+
+ /**
* The SHA3-224 hash algorithm defined in the FIPS PUB 202.
* <p>
- * Included starting in Oracle Java 9 GA.
+ * Included starting in Oracle Java 9.
* </p>
*
* @since 1.11
@@ -106,7 +126,7 @@ public class MessageDigestAlgorithms {
/**
* The SHA3-256 hash algorithm defined in the FIPS PUB 202.
* <p>
- * Included starting in Oracle Java 9 GA.
+ * Included starting in Oracle Java 9.
* </p>
*
* @since 1.11
@@ -116,7 +136,7 @@ public class MessageDigestAlgorithms {
/**
* The SHA3-384 hash algorithm defined in the FIPS PUB 202.
* <p>
- * Included starting in Oracle Java 9 GA.
+ * Included starting in Oracle Java 9.
* </p>
*
* @since 1.11
@@ -126,7 +146,7 @@ public class MessageDigestAlgorithms {
/**
* The SHA3-512 hash algorithm defined in the FIPS PUB 202.
* <p>
- * Included starting in Oracle Java 9 GA.
+ * Included starting in Oracle Java 9.
* </p>
*
* @since 1.11
@@ -142,7 +162,7 @@ public class MessageDigestAlgorithms {
public static String[] values() {
// N.B. do not use a constant array here as that can be changed externally by accident or design
return new String[] {
- MD2, MD5, SHA_1, SHA_224, SHA_256, SHA_384, SHA_512, SHA3_224, SHA3_256, SHA3_384, SHA3_512
+ MD2, MD5, SHA_1, SHA_224, SHA_256, SHA_384, SHA_512, SHA_512_224, SHA_512_256, SHA3_224, SHA3_256, SHA3_384, SHA3_512
};
}
diff --git a/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java b/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
index c5002be..c5766bd 100644
--- a/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
+++ b/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.codec.digest;
import static org.apache.commons.codec.binary.StringUtils.getBytesUtf8;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
import java.io.ByteArrayInputStream;
import java.io.File;
@@ -29,6 +30,7 @@ import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
+import java.util.Locale;
import java.util.Random;
import org.apache.commons.codec.binary.Hex;
@@ -357,6 +359,53 @@ public class DigestUtilsTest {
}
@Test
+ public void testSha512_224() throws Exception {
+ assumeJava9();
+ // Examples from
+ // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
+ final String stringInput = "abc";
+ final byte[] bytesInput = getBytesUtf8(stringInput);
+ final String resultString = "4634270F707B6A54DAAE7530460842E20E37ED265CEEE9A43E8924AA".toLowerCase(Locale.ROOT);
+ final byte[] resultBytes = Hex.decodeHex(resultString);
+ //
+ assertArrayEquals(resultBytes, DigestUtils.sha512_224(bytesInput));
+ assertArrayEquals(resultBytes, DigestUtils.sha512_224(new ByteArrayInputStream(bytesInput)));
+ assertArrayEquals(resultBytes, DigestUtils.sha512_224(stringInput));
+ //
+ assertEquals(resultString, DigestUtils.sha512_224Hex(bytesInput));
+ assertEquals(resultString, DigestUtils.sha512_224Hex(new ByteArrayInputStream(bytesInput)));
+ assertEquals(resultString, DigestUtils.sha512_224Hex(stringInput));
+ // Example 2
+ assertEquals("23FEC5BB94D60B23308192640B0C453335D664734FE40E7268674AF9".toLowerCase(Locale.ROOT),
+ DigestUtils.sha512_224Hex(
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
+ }
+
+ @Test
+ public void testSha512_256() throws Exception {
+ assumeJava9();
+ // Examples from
+ // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
+ final String stringInput = "abc";
+ final byte[] bytesInput = getBytesUtf8(stringInput);
+ final String resultString = "53048E2681941EF99B2E29B76B4C7DABE4C2D0C634FC6D46E0E2F13107E7AF23"
+ .toLowerCase(Locale.ROOT);
+ final byte[] resultBytes = Hex.decodeHex(resultString);
+ //
+ assertArrayEquals(resultBytes, DigestUtils.sha512_256(bytesInput));
+ assertArrayEquals(resultBytes, DigestUtils.sha512_256(new ByteArrayInputStream(bytesInput)));
+ assertArrayEquals(resultBytes, DigestUtils.sha512_256(stringInput));
+ //
+ assertEquals(resultString, DigestUtils.sha512_256Hex(bytesInput));
+ assertEquals(resultString, DigestUtils.sha512_256Hex(new ByteArrayInputStream(bytesInput)));
+ assertEquals(resultString, DigestUtils.sha512_256Hex(stringInput));
+ // Example 2
+ assertEquals("3928E184FB8690F840DA3988121D31BE65CB9D3EF83EE6146FEAC861E19B563A".toLowerCase(Locale.ROOT),
+ DigestUtils.sha512_256Hex(
+ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
+ }
+
+ @Test
public void testSha3_224() {
assumeJava9();
// Examples from https://csrc.nist.gov/projects/cryptographic-standards-and-guidelines/example-values