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 2014/11/10 17:12:41 UTC
svn commit: r1637907 - 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
Author: ggregory
Date: Mon Nov 10 16:12:39 2014
New Revision: 1637907
URL: http://svn.apache.org/r1637907
Log:
[CODEC-193] Support java.nio.ByteBuffer in DigestUtils
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=1637907&r1=1637906&r2=1637907&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Mon Nov 10 16:12:39 2014
@@ -44,6 +44,7 @@ The <action> type attribute can be add,u
<body>
<release version="1.11" date="DD MM 2014" description="Feature and fix release.">
<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>
<release version="1.10" date="5 November 2014" description="Feature and fix release.">
<action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add Daitch-Mokotoff Soundex</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=1637907&r1=1637906&r2=1637907&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:12:39 2014
@@ -19,6 +19,7 @@ package org.apache.commons.codec.digest;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -34,6 +35,22 @@ import org.apache.commons.codec.binary.S
public class DigestUtils {
/**
+ * Read through an ByteBuffer and returns the digest for the data
+ *
+ * @param digest
+ * The MessageDigest to use (e.g. MD5)
+ * @param data
+ * Data to digest
+ * @return the digest
+ * @throws IOException
+ * On error reading from the stream
+ */
+ private static byte[] digest(final MessageDigest messageDigest, final ByteBuffer data) {
+ messageDigest.update(data);
+ return messageDigest.digest();
+ }
+
+ /**
* Read through an InputStream and returns the digest for the data
*
* @param digest
@@ -227,6 +244,18 @@ public class DigestUtils {
* @param data
* Data to digest
* @return MD2 digest as a hex string
+ * @since 1.11
+ */
+ public static String md2Hex(final ByteBuffer 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
@@ -259,6 +288,78 @@ 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.11
+ */
+ public static byte[] md2(final ByteBuffer data) {
+ return digest(getMd2Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-1 digest
+ * @since 1.11
+ */
+ public static byte[] sha1(final ByteBuffer data) {
+ return digest(getSha1Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-256 digest and returns the value as a <code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-256 digest
+ * @since 1.11
+ */
+ public static byte[] sha256(final ByteBuffer data) {
+ return digest(getSha256Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-384 digest
+ * @since 1.11
+ */
+ public static byte[] sha384(final ByteBuffer data) {
+ return digest(getSha384Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-512 digest and returns the value as a <code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512 digest
+ * @since 1.11
+ */
+ public static byte[] sha512(final ByteBuffer data) {
+ return digest(getSha512Digest(), data);
+ }
+
+ /**
+ * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return MD5 digest
+ * @since 1.11
+ */
+ public static byte[] md5(final ByteBuffer data) {
+ return digest(getMd5Digest(), data);
+ }
+
+ /**
* Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>.
*
* @param data
@@ -300,6 +401,18 @@ public class DigestUtils {
* @param data
* Data to digest
* @return MD5 digest as a hex string
+ * @since 1.11
+ */
+ public static String md5Hex(final ByteBuffer data) {
+ return Hex.encodeHexString(md5(data));
+ }
+
+/**
+ * Calculates the MD5 digest and returns the value as a 32 character hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return MD5 digest as a hex string
* @throws IOException
* On error reading from the stream
* @since 1.4
@@ -416,6 +529,18 @@ public class DigestUtils {
* @param data
* Data to digest
* @return SHA-1 digest as a hex string
+ * @since 1.11
+ */
+ public static String sha1Hex(final ByteBuffer 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
@@ -500,6 +625,18 @@ public class DigestUtils {
/**
* Calculates the SHA-256 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-256 digest as a hex string
+ * @since 1.11
+ */
+ public static String sha256Hex(final ByteBuffer data) {
+ return Hex.encodeHexString(sha256(data));
+ }
+
+ /**
+ * Calculates the SHA-256 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
@@ -594,6 +731,18 @@ public class DigestUtils {
/**
* Calculates the SHA-384 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-384 digest as a hex string
+ * @since 1.11
+ */
+ public static String sha384Hex(final ByteBuffer data) {
+ return Hex.encodeHexString(sha384(data));
+ }
+
+ /**
+ * Calculates the SHA-384 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
@@ -688,6 +837,18 @@ public class DigestUtils {
/**
* Calculates the SHA-512 digest and returns the value as a hex string.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-512 digest as a hex string
+ * @since 1.11
+ */
+ public static String sha512Hex(final ByteBuffer data) {
+ return Hex.encodeHexString(sha512(data));
+ }
+
+ /**
+ * Calculates the SHA-512 digest and returns the value as a hex string.
* <p>
* Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
* </p>
@@ -776,6 +937,21 @@ public class DigestUtils {
}
/**
+ * Updates the given {@link MessageDigest}.
+ *
+ * @param messageDigest
+ * the {@link MessageDigest} to update
+ * @param valueToDigest
+ * the value to update the {@link MessageDigest} with
+ * @return the updated {@link MessageDigest}
+ * @since 1.11
+ */
+ public static MessageDigest updateDigest(final MessageDigest messageDigest, final ByteBuffer valueToDigest) {
+ messageDigest.update(valueToDigest);
+ return messageDigest;
+ }
+
+ /**
* Reads through an InputStream and updates the digest for the data
*
* @param digest
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=1637907&r1=1637906&r2=1637907&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:12:39 2014
@@ -18,11 +18,13 @@
package org.apache.commons.codec.digest;
import static org.apache.commons.codec.binary.StringUtils.getBytesUtf8;
+import static org.apache.commons.codec.binary.StringUtils.getByteBufferUtf8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.util.Random;
@@ -79,7 +81,10 @@ public class DigestUtilsTest {
assertEquals(DigestUtils.md2Hex(testData),
DigestUtils.md2Hex(new ByteArrayInputStream(testData)));
- }
+
+ assertEquals(DigestUtils.md2Hex(testData),
+ DigestUtils.md2Hex(ByteBuffer.wrap(testData)));
+}
/**
* An MD2 hash converted to hex should always be 32 characters.
@@ -132,13 +137,16 @@ public class DigestUtilsTest {
assertEquals(DigestUtils.md5Hex(testData),
DigestUtils.md5Hex(new ByteArrayInputStream(testData)));
- }
+
+ assertEquals(DigestUtils.md5Hex(testData),
+ DigestUtils.md5Hex(ByteBuffer.wrap(testData)));
+}
/**
* An MD5 hash converted to hex should always be 32 characters.
*/
@Test
- public void testMd5HexLength() {
+ public void testMd5HexLengthForBytes() {
String hashMe = "this is some string that is longer than 32 characters";
String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe));
assertEquals(32, hash.length());
@@ -149,10 +157,24 @@ public class DigestUtilsTest {
}
/**
+ * An MD5 hash converted to hex should always be 32 characters.
+ */
+ @Test
+ public void testMd5HexLengthForByteBuffer() {
+ String hashMe = "this is some string that is longer than 32 characters";
+ String hash = DigestUtils.md5Hex(getByteBufferUtf8(hashMe));
+ assertEquals(32, hash.length());
+
+ hashMe = "length < 32";
+ hash = DigestUtils.md5Hex(getByteBufferUtf8(hashMe));
+ assertEquals(32, hash.length());
+ }
+
+ /**
* An MD5 hash should always be a 16 element byte[].
*/
@Test
- public void testMd5Length() {
+ public void testMd5LengthForBytes() {
String hashMe = "this is some string that is longer than 16 characters";
byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe));
assertEquals(16, hash.length);
@@ -162,6 +184,20 @@ public class DigestUtilsTest {
assertEquals(16, hash.length);
}
+ /**
+ * An MD5 hash should always be a 16 element byte[].
+ */
+ @Test
+ public void testMd5LengthForByteBuffer() {
+ String hashMe = "this is some string that is longer than 16 characters";
+ byte[] hash = DigestUtils.md5(getByteBufferUtf8(hashMe));
+ assertEquals(16, hash.length);
+
+ hashMe = "length < 16";
+ hash = DigestUtils.md5(getByteBufferUtf8(hashMe));
+ assertEquals(16, hash.length);
+ }
+
@Test
public void testSha1Hex() throws IOException {
// Examples from FIPS 180-1
@@ -174,6 +210,8 @@ public class DigestUtilsTest {
DigestUtils.sha1Hex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq"));
assertEquals(DigestUtils.sha1Hex(testData),
DigestUtils.sha1Hex(new ByteArrayInputStream(testData)));
+ assertEquals(DigestUtils.sha1Hex(testData),
+ DigestUtils.sha1Hex(ByteBuffer.wrap(testData)));
}
@Test
@@ -195,6 +233,24 @@ public class DigestUtilsTest {
}
@Test
+ public void testSha1UpdateWithByteBuffer(){
+ 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!'";
+
+ MessageDigest messageDigest = DigestUtils.getSha1Digest();
+ messageDigest.update(d1.getBytes());
+ messageDigest.update(d2.getBytes());
+ final String expectedResult = Hex.encodeHexString(messageDigest.digest());
+
+ messageDigest = DigestUtils.getSha1Digest();
+ DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d1.getBytes()));
+ DigestUtils.updateDigest(messageDigest, ByteBuffer.wrap(d2.getBytes()));
+ final String actualResult = Hex.encodeHexString(messageDigest.digest());
+
+ assertEquals(expectedResult, actualResult);
+ }
+
+ @Test
public void testSha1UpdateWithString(){
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!'";
@@ -224,6 +280,8 @@ public class DigestUtilsTest {
assertEquals(DigestUtils.sha256Hex(testData),
DigestUtils.sha256Hex(new ByteArrayInputStream(testData)));
+ assertEquals(DigestUtils.sha256Hex(testData),
+ DigestUtils.sha256Hex(ByteBuffer.wrap(testData)));
}
@Test
@@ -241,6 +299,8 @@ public class DigestUtilsTest {
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
assertEquals(DigestUtils.sha384Hex(testData),
DigestUtils.sha384Hex(new ByteArrayInputStream(testData)));
+ assertEquals(DigestUtils.sha384Hex(testData),
+ DigestUtils.sha384Hex(ByteBuffer.wrap(testData)));
}
@Test
@@ -258,6 +318,8 @@ public class DigestUtilsTest {
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
assertEquals(DigestUtils.sha512Hex(testData),
DigestUtils.sha512Hex(new ByteArrayInputStream(testData)));
+ assertEquals(DigestUtils.sha512Hex(testData),
+ DigestUtils.sha512Hex(ByteBuffer.wrap(testData)));
}
@SuppressWarnings("deprecation") // deliberate tests of deprecated code