Author: ggregory
Date: Fri May 13 20:49:05 2016
New Revision: 1743756
URL: http://svn.apache.org/viewvc?rev=1743756&view=rev
Log:
[CODEC-206] Add java.io.File APIs to 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=1743756&r1=1743755&r2=1743756&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/changes/changes.xml (original)
+++ commons/proper/codec/trunk/src/changes/changes.xml Fri May 13 20:49:05 2016
@@ -46,6 +46,7 @@ The <action> type attribute can be add,u
<action dev="sebb" type="fix" issue="CODEC-200" due-to="Luciano
Vernaschi">Base32.HEX_DECODE_TABLE contains the wrong value 32</action>
<action dev="ggregory" type="fix" issue="CODEC-207" due-to="Gary
Gregory">Charsets Javadoc breaks build when using Java 8</action>
<action dev="ggregory" type="fix" issue="CODEC-199" due-to="Yossi
Tamari">Bug in HW rule in Soundex</action>
+ <action dev="ggregory" type="add" issue="CODEC-206" due-to="Gary
Gregory">Add java.io.File APIs to DigestUtils</action>
<action dev="ggregory" type="add" issue="CODEC-183" due-to="Steven
Wurster">BaseNCodecOutputStream only supports writing EOF on close()</action>
<action dev="ggregory" type="add" issue="CODEC-195" due-to="Gary
Gregory">Support SHA-224 in DigestUtils on Java 8</action>
<action dev="ggregory" type="add" issue="CODEC-194" due-to="Gary
Gregory">Support java.nio.ByteBuffer in
org.apache.commons.codec.binary.Hex</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=1743756&r1=1743755&r2=1743756&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
Fri May 13 20:49:05 2016
@@ -17,6 +17,9 @@
package org.apache.commons.codec.digest;
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
@@ -53,6 +56,21 @@ public class DigestUtils {
}
/**
+ * Read through a File 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 digest, final File data)
throws IOException {
+ return updateDigest(digest, data).digest();
+ }
+
+ /**
* Read through an InputStream and returns the digest for the data
*
* @param digest
@@ -238,6 +256,20 @@ public class DigestUtils {
* @return MD2 digest
* @throws IOException
* On error reading from the stream
+ * @since 1.11
+ */
+ public static byte[] md2(final File data) throws IOException {
+ return digest(getMd2Digest(), data);
+ }
+
+ /**
+ * Calculates the MD2 digest and returns the value as a 16 element
<code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return MD2 digest
+ * @throws IOException
+ * On error reading from the stream
* @since 1.7
*/
public static byte[] md2(final InputStream data) throws IOException {
@@ -288,6 +320,20 @@ public class DigestUtils {
* @return MD2 digest as a hex string
* @throws IOException
* On error reading from the stream
+ * @since 1.11
+ */
+ public static String md2Hex(final File data) throws IOException {
+ 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
*/
public static String md2Hex(final InputStream data) throws IOException {
@@ -337,6 +383,20 @@ public class DigestUtils {
* @return MD5 digest
* @throws IOException
* On error reading from the stream
+ * @since 1.11
+ */
+ public static byte[] md5(final File data) throws IOException {
+ return digest(getMd5Digest(), data);
+ }
+
+ /**
+ * Calculates the MD5 digest and returns the value as a 16 element
<code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return MD5 digest
+ * @throws IOException
+ * On error reading from the stream
* @since 1.4
*/
public static byte[] md5(final InputStream data) throws IOException {
@@ -378,18 +438,18 @@ public class DigestUtils {
}
/**
- * 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
- */
- public static String md5Hex(final InputStream data) throws IOException
{
- 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.11
+ */
+ public static String md5Hex(final File data) throws IOException {
+ return Hex.encodeHexString(md5(data));
+ }
/**
* Calculates the MD5 digest and returns the value as a 32 character hex
string.
@@ -397,37 +457,35 @@ public class DigestUtils {
* @param data
* Data to digest
* @return MD5 digest as a hex string
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.4
*/
- public static String md5Hex(final String data) {
+ public static String md5Hex(final InputStream data) throws IOException {
return Hex.encodeHexString(md5(data));
}
/**
- * Calculates the SHA-1 digest and returns the value as a
<code>byte[]</code>.
+ * Calculates the MD5 digest and returns the value as a 32 character hex
string.
*
* @param data
* Data to digest
- * @return SHA-1 digest
- * @deprecated Use {@link #sha1(byte[])}
+ * @return MD5 digest as a hex string
*/
- @Deprecated
- public static byte[] sha(final byte[] data) {
- return sha1(data);
+ public static String md5Hex(final String data) {
+ return Hex.encodeHexString(md5(data));
}
-/**
+ /**
* Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>.
*
* @param data
* Data to digest
* @return SHA-1 digest
- * @throws IOException
- * On error reading from the stream
- * @since 1.4
- * @deprecated Use {@link #sha1(InputStream)}
+ * @deprecated Use {@link #sha1(byte[])}
*/
@Deprecated
-public static byte[] sha(final InputStream data) throws IOException {
+public static byte[] sha(final byte[] data) {
return sha1(data);
}
@@ -437,6 +495,22 @@ public static byte[] sha(final InputStre
* @param data
* Data to digest
* @return SHA-1 digest
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.4
+ * @deprecated Use {@link #sha1(InputStream)}
+ */
+ @Deprecated
+ public static byte[] sha(final InputStream data) throws IOException {
+ return sha1(data);
+ }
+
+ /**
+ * Calculates the SHA-1 digest and returns the value as a
<code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-1 digest
* @deprecated Use {@link #sha1(String)}
*/
@Deprecated
@@ -476,6 +550,20 @@ public static byte[] sha(final InputStre
* @return SHA-1 digest
* @throws IOException
* On error reading from the stream
+ * @since 1.11
+ */
+ public static byte[] sha1(final File data) throws IOException {
+ return digest(getSha1Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-1 digest and returns the value as a
<code>byte[]</code>.
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-1 digest
+ * @throws IOException
+ * On error reading from the stream
* @since 1.7
*/
public static byte[] sha1(final InputStream data) throws IOException {
@@ -525,6 +613,20 @@ public static byte[] sha(final InputStre
* @return SHA-1 digest as a hex string
* @throws IOException
* On error reading from the stream
+ * @since 1.11
+ */
+ public static String sha1Hex(final File 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
+ * @throws IOException
+ * On error reading from the stream
* @since 1.7
*/
public static String sha1Hex(final InputStream data) throws IOException {
@@ -575,6 +677,23 @@ public static byte[] sha(final InputStre
/**
* Calculates the SHA-224 digest and returns the value as a
<code>byte[]</code>.
* <p>
+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
+ * </p>
+ *
+ * @param data
+ * File to digest
+ * @return SHA-224 digest
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.11
+ */
+ public static byte[] sha224(final File 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>
*
@@ -652,6 +771,24 @@ public static byte[] sha(final InputStre
* @throws IllegalArgumentException thrown on JRE versions prior to 1.8.0.
* @since 1.11
*/
+ public static String sha224Hex(final File 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 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));
}
@@ -671,7 +808,7 @@ public static byte[] sha(final InputStre
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>
@@ -706,6 +843,23 @@ public static byte[] sha(final InputStre
* </p>
*
* @param data
+ * File to digest
+ * @return SHA-256 digest
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.11
+ */
+ public static byte[] sha256(final File data) throws IOException {
+ return digest(getSha256Digest(), 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.
+ * </p>
+ *
+ * @param data
* Data to digest
* @return SHA-256 digest
* @throws IOException
@@ -769,6 +923,23 @@ public static byte[] sha(final InputStre
* @return SHA-256 digest as a hex string
* @throws IOException
* On error reading from the stream
+ * @since 1.11
+ */
+ public static String sha256Hex(final File data) throws IOException {
+ 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>
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-256 digest as a hex string
+ * @throws IOException
+ * On error reading from the stream
* @since 1.4
*/
public static String sha256Hex(final InputStream data) throws IOException {
@@ -824,6 +995,23 @@ public static byte[] sha(final InputStre
* </p>
*
* @param data
+ * File to digest
+ * @return SHA-384 digest
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.11
+ */
+ public static byte[] sha384(final File data) throws IOException {
+ return digest(getSha384Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-384 digest and returns the value as a
<code>byte[]</code>.
+ * <p>
+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
+ * </p>
+ *
+ * @param data
* Data to digest
* @return SHA-384 digest
* @throws IOException
@@ -887,6 +1075,23 @@ public static byte[] sha(final InputStre
* @return SHA-384 digest as a hex string
* @throws IOException
* On error reading from the stream
+ * @since 1.11
+ */
+ public static String sha384Hex(final File data) throws IOException {
+ 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>
+ *
+ * @param data
+ * Data to digest
+ * @return SHA-384 digest as a hex string
+ * @throws IOException
+ * On error reading from the stream
* @since 1.4
*/
public static String sha384Hex(final InputStream data) throws IOException {
@@ -942,6 +1147,23 @@ public static byte[] sha(final InputStre
* </p>
*
* @param data
+ * File to digest
+ * @return SHA-512 digest
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.11
+ */
+ public static byte[] sha512(final File data) throws IOException {
+ return digest(getSha512Digest(), data);
+ }
+
+ /**
+ * Calculates the SHA-512 digest and returns the value as a
<code>byte[]</code>.
+ * <p>
+ * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0.
+ * </p>
+ *
+ * @param data
* Data to digest
* @return SHA-512 digest
* @throws IOException
@@ -1001,6 +1223,23 @@ public static byte[] sha(final InputStre
* </p>
*
* @param data
+ * File to digest
+ * @return SHA-512 digest as a hex string
+ * @throws IOException
+ * On error reading from the stream
+ * @since 1.11
+ */
+ public static String sha512Hex(File data) throws IOException {
+ 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>
+ *
+ * @param data
* Data to digest
* @return SHA-512 digest as a hex string
* @throws IOException
@@ -1099,6 +1338,27 @@ public static byte[] sha(final InputStre
}
/**
+ * Reads through a File and updates 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
+ * @since 1.11
+ */
+ public static MessageDigest updateDigest(final MessageDigest digest, final
File data) throws IOException {
+ final BufferedInputStream stream = new BufferedInputStream(new
FileInputStream(data));
+ try {
+ return updateDigest(digest, stream);
+ } finally {
+ stream.close();
+ }
+ }
+
+ /**
* Reads through an InputStream and updates the digest for the data
*
* @param digest
@@ -1137,4 +1397,5 @@ public static byte[] sha(final InputStre
messageDigest.update(StringUtils.getBytesUtf8(valueToDigest));
return messageDigest;
}
+
}
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=1743756&r1=1743755&r2=1743756&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
Fri May 13 20:49:05 2016
@@ -17,12 +17,14 @@
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.apache.commons.codec.binary.StringUtils.getBytesUtf8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
@@ -32,7 +34,9 @@ import org.apache.commons.codec.binary.H
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.lang3.JavaVersion;
import org.apache.commons.lang3.SystemUtils;
+import org.junit.After;
import org.junit.Assume;
+import org.junit.Before;
import org.junit.Test;
/**
@@ -43,12 +47,23 @@ import org.junit.Test;
public class DigestUtilsTest {
private final byte[] testData = new byte[1024 * 1024];
+
+ private File testFile;
- /* (non-Javadoc)
- * @see junit.framework.TestCase#setUp()
- */
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
new Random().nextBytes(testData);
+ testFile = File.createTempFile(DigestUtilsTest.class.getName(),
".dat");
+ FileOutputStream fos = new FileOutputStream(testFile);
+ fos.write(testData);
+ fos.close();
+ }
+
+ @After
+ public void tearDown() {
+ if (!testFile.delete()) {
+ testFile.deleteOnExit();
+ }
}
@Test
@@ -89,6 +104,11 @@ public class DigestUtilsTest {
DigestUtils.md2Hex(ByteBuffer.wrap(testData)));
}
+ @Test
+ public void testMd2HexFile() throws IOException {
+ assertEquals(DigestUtils.md2Hex(testData),
DigestUtils.md2Hex(testFile));
+ }
+
/**
* An MD2 hash converted to hex should always be 32 characters.
*/
@@ -145,6 +165,11 @@ public class DigestUtilsTest {
DigestUtils.md5Hex(ByteBuffer.wrap(testData)));
}
+ @Test
+ public void testMd5HexFile() throws IOException {
+ assertEquals(DigestUtils.md5Hex(testData),
DigestUtils.md5Hex(testFile));
+ }
+
/**
* An MD5 hash converted to hex should always be 32 characters.
*/
@@ -218,6 +243,11 @@ public class DigestUtilsTest {
}
@Test
+ public void testSha1HexFile() throws IOException {
+ assertEquals(DigestUtils.sha1Hex(testData),
DigestUtils.sha1Hex(testFile));
+ }
+
+ @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!'";
@@ -273,7 +303,7 @@ public class DigestUtilsTest {
@Test
public void testSha224() throws IOException {
-
Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
+ assumeJava8();
assertEquals("d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
DigestUtils.sha224Hex(""));
assertEquals("730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525",
DigestUtils.sha224Hex("The quick brown fox jumps over the lazy
dog"));
@@ -281,6 +311,16 @@ public class DigestUtilsTest {
// Examples from FIPS 180-4?
}
+ private void assumeJava8() {
+
Assume.assumeTrue(SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_1_8));
+ }
+
+ @Test
+ public void testSha224HexFile() throws IOException {
+ assumeJava8();
+ assertEquals(DigestUtils.sha224Hex(testData),
DigestUtils.sha224Hex(testFile));
+ }
+
@Test
public void testSha256() throws IOException {
// Examples from FIPS 180-2
@@ -298,6 +338,11 @@ public class DigestUtilsTest {
}
@Test
+ public void testSha256HexFile() throws IOException {
+ assertEquals(DigestUtils.sha256Hex(testData),
DigestUtils.sha256Hex(testFile));
+ }
+
+ @Test
public void testSha384() throws IOException {
// Examples from FIPS 180-2
assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"
+
@@ -317,6 +362,11 @@ public class DigestUtilsTest {
}
@Test
+ public void testSha384HexFile() throws IOException {
+ assertEquals(DigestUtils.sha384Hex(testData),
DigestUtils.sha384Hex(testFile));
+ }
+
+ @Test
public void testSha512() throws IOException {
// Examples from FIPS 180-2
assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
+
@@ -329,10 +379,22 @@ public class DigestUtilsTest {
"501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
+
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"));
- assertEquals(DigestUtils.sha512Hex(testData),
- DigestUtils.sha512Hex(new ByteArrayInputStream(testData)));
- assertEquals(DigestUtils.sha512Hex(testData),
- DigestUtils.sha512Hex(ByteBuffer.wrap(testData)));
+ }
+
+ @Test
+ public void testSha512HexByteBuffer() throws IOException {
+ assertEquals(DigestUtils.sha512Hex(testData),
DigestUtils.sha512Hex(ByteBuffer.wrap(testData)));
+ }
+
+ @Test
+ public void testSha512HexFile() throws IOException {
+ assertEquals(DigestUtils.sha512Hex(testData),
DigestUtils.sha512Hex(testFile));
+ }
+
+ @Test
+ public void testSha512HexInputStream() throws IOException {
+ assertEquals(DigestUtils.sha512Hex(testData),
+ DigestUtils.sha512Hex(new ByteArrayInputStream(testData)));
}
@SuppressWarnings("deprecation") // deliberate tests of deprecated code