Author: bayard Date: Fri Mar 7 23:49:46 2008 New Revision: 634903 URL: http://svn.apache.org/viewvc?rev=634903&view=rev Log: Applying CODEC-52; Niklas Gustavsson's enhancement to have InputStream variants for DigestUtil's digest methods
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java?rev=634903&r1=634902&r2=634903&view=diff ============================================================================== --- commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java (original) +++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java Fri Mar 7 23:49:46 2008 @@ -17,6 +17,8 @@ package org.apache.commons.codec.digest; +import java.io.IOException; +import java.io.InputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -30,6 +32,8 @@ */ public class DigestUtils { + private static final int STREAM_BUFFER_LENGTH = 1024; + /** * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>. * @@ -116,6 +120,26 @@ } /** + * Read through an InputStream and returns the digest for the data + * + * @param digest The MessageDigest to use (e.g. MD5) + * @param data Data to digest + * @return MD5 digest + * @throws IOException On error reading from the stream + */ + private static byte[] digest(MessageDigest digest, InputStream data) throws IOException { + byte[] buffer = new byte[STREAM_BUFFER_LENGTH]; + int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); + + while(read > -1) { + digest.update(buffer, 0, read); + read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); + } + + return digest.digest(); + } + + /** * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. * * @param data @@ -132,6 +156,18 @@ * @param data * Data to digest * @return MD5 digest + * @throws IOException On error reading from the stream + */ + public static byte[] md5(InputStream 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 */ public static byte[] md5(String data) { return md5(data.getBytes()); @@ -160,6 +196,18 @@ } /** + * 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 + */ + public static String md5Hex(InputStream data) throws IOException { + return new String(Hex.encodeHex(md5(data))); + } + + /** * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. * * @param data @@ -182,6 +230,18 @@ } /** + * 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 + */ + public static byte[] sha(InputStream data) throws IOException { + return digest(getShaDigest(), 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. @@ -210,6 +270,21 @@ } /** + * 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 On error reading from the stream + */ + public static byte[] sha256(InputStream data) throws IOException { + return digest(getSha256Digest(), 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. @@ -238,6 +313,21 @@ } /** + * 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 + */ + public static String sha256Hex(InputStream data) throws IOException { + return new String(Hex.encodeHex(sha256(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. @@ -265,6 +355,21 @@ public static byte[] sha384(String data) { return sha384(data.getBytes()); } + + /** + * 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 On error reading from the stream + */ + public static byte[] sha384(InputStream data) throws IOException { + return digest(getSha384Digest(), data); + } /** * Calculates the SHA-384 digest and returns the value as a hex string. @@ -295,6 +400,21 @@ } /** + * 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 + */ + public static String sha384Hex(InputStream data) throws IOException { + return new String(Hex.encodeHex(sha384(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. @@ -323,6 +443,21 @@ } /** + * 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 On error reading from the stream + */ + public static byte[] sha512(InputStream data) throws IOException { + return digest(getSha512Digest(), 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. @@ -351,6 +486,21 @@ } /** + * 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 On error reading from the stream + */ + public static String sha512Hex(InputStream data) throws IOException { + return new String(Hex.encodeHex(sha512(data))); + } + + /** * Calculates the SHA-1 digest and returns the value as a hex string. * * @param data @@ -369,6 +519,18 @@ * @return SHA-1 digest as a hex string */ public static String shaHex(String data) { + return new String(Hex.encodeHex(sha(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 + */ + public static String shaHex(InputStream data) throws IOException { return new String(Hex.encodeHex(sha(data))); } } Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java?rev=634903&r1=634902&r2=634903&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java (original) +++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java Fri Mar 7 23:49:46 2008 @@ -17,6 +17,10 @@ package org.apache.commons.codec.digest; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Random; + import junit.framework.TestCase; /** @@ -27,6 +31,15 @@ */ public class DigestUtilsTest extends TestCase { + private byte[] testData = new byte[1024*1024]; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + new Random().nextBytes(testData); + } + public void testInternalNoSuchAlgorithmException() { try { DigestUtils.getDigest("Bogus Bogus"); @@ -36,7 +49,7 @@ } } - public void testMd5Hex() { + public void testMd5Hex() throws IOException { // Examples from RFC 1321 assertEquals("d41d8cd98f00b204e9800998ecf8427e", DigestUtils.md5Hex("")); @@ -55,8 +68,11 @@ assertEquals( "57edf4a22be3c955ac49da2e2107b67a", DigestUtils.md5Hex("1234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890")); - } + assertEquals(DigestUtils.md5Hex(testData), + DigestUtils.md5Hex(new ByteArrayInputStream(testData))); + } + /** * An MD5 hash converted to hex should always be 32 characters. */ @@ -83,7 +99,7 @@ assertEquals(16, hash.length); } - public void testSha256() { + public void testSha256() throws IOException { // Examples from FIPS 180-2 assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex("abc")); @@ -91,9 +107,12 @@ DigestUtils.sha256Hex("abc".getBytes())); assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); + + assertEquals(DigestUtils.sha256Hex(testData), + DigestUtils.sha256Hex(new ByteArrayInputStream(testData))); } - public void testSha384() { + public void testSha384() throws IOException { // Examples from FIPS 180-2 assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + "8086072ba1e7cc2358baeca134c825a7", @@ -105,9 +124,11 @@ "fcc7c71a557e2db966c3e9fa91746039", DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); + assertEquals(DigestUtils.sha384Hex(testData), + DigestUtils.sha384Hex(new ByteArrayInputStream(testData))); } - public void testSha512() { + public void testSha512() throws IOException { // Examples from FIPS 180-2 assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", @@ -119,9 +140,11 @@ "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); - } + assertEquals(DigestUtils.sha512Hex(testData), + DigestUtils.sha512Hex(new ByteArrayInputStream(testData))); +} - public void testShaHex() { + public void testShaHex() throws IOException { // Examples from FIPS 180-1 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc")); @@ -130,5 +153,8 @@ assertEquals( "84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); + assertEquals(DigestUtils.shaHex(testData), + DigestUtils.shaHex(new ByteArrayInputStream(testData))); + } }