Author: ggregory Date: Mon Aug 3 09:32:08 2009 New Revision: 800270 URL: http://svn.apache.org/viewvc?rev=800270&view=rev Log: [CODEC-73] DigestUtils: Make string2byte conversions indepedent of platform default encoding.
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=800270&r1=800269&r2=800270&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 Mon Aug 3 09:32:08 2009 @@ -23,6 +23,7 @@ import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; +import org.apache.commons.codec.binary.StringUtils; /** * Operations to simplifiy common {...@link java.security.MessageDigest} tasks. This class is thread safe. @@ -37,24 +38,38 @@ /** * 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 + * @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 + * @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) { + + while (read > -1) { digest.update(buffer, 0, read); read = data.read(buffer, 0, STREAM_BUFFER_LENGTH); } - + return digest.digest(); } /** + * Calls {...@link StringUtils#getBytesUtf8(String)} + * + * @param string + * the String to encode + * @return encoded bytes + */ + private static byte[] getBytesUtf8(String data) { + return StringUtils.getBytesUtf8(data); + } + + /** * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>. * * @param algorithm @@ -138,7 +153,7 @@ private static MessageDigest getShaDigest() { return getDigest("SHA"); } - + /** * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. * @@ -156,7 +171,8 @@ * @param data * Data to digest * @return MD5 digest - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static byte[] md5(InputStream data) throws IOException { @@ -171,7 +187,7 @@ * @return MD5 digest */ public static byte[] md5(String data) { - return md5(data.getBytes()); + return md5(getBytesUtf8(data)); } /** @@ -191,7 +207,8 @@ * @param data * Data to digest * @return MD5 digest as a hex string - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static String md5Hex(InputStream data) throws IOException { @@ -226,7 +243,8 @@ * @param data * Data to digest * @return SHA-1 digest - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream */ public static byte[] sha(InputStream data) throws IOException { return digest(getShaDigest(), data); @@ -240,7 +258,7 @@ * @return SHA-1 digest */ public static byte[] sha(String data) { - return sha(data.getBytes()); + return sha(getBytesUtf8(data)); } /** @@ -267,7 +285,8 @@ * @param data * Data to digest * @return SHA-256 digest - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static byte[] sha256(InputStream data) throws IOException { @@ -286,7 +305,7 @@ * @since 1.4 */ public static byte[] sha256(String data) { - return sha256(data.getBytes()); + return sha256(getBytesUtf8(data)); } /** @@ -313,7 +332,8 @@ * @param data * Data to digest * @return SHA-256 digest as a hex string - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static String sha256Hex(InputStream data) throws IOException { @@ -359,13 +379,14 @@ * @param data * Data to digest * @return SHA-384 digest - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static byte[] sha384(InputStream data) throws IOException { return digest(getSha384Digest(), data); } - + /** * Calculates the SHA-384 digest and returns the value as a <code>byte[]</code>. * <p> @@ -378,7 +399,7 @@ * @since 1.4 */ public static byte[] sha384(String data) { - return sha384(data.getBytes()); + return sha384(getBytesUtf8(data)); } /** @@ -405,7 +426,8 @@ * @param data * Data to digest * @return SHA-384 digest as a hex string - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static String sha384Hex(InputStream data) throws IOException { @@ -451,7 +473,8 @@ * @param data * Data to digest * @return SHA-512 digest - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static byte[] sha512(InputStream data) throws IOException { @@ -470,7 +493,7 @@ * @since 1.4 */ public static byte[] sha512(String data) { - return sha512(data.getBytes()); + return sha512(getBytesUtf8(data)); } /** @@ -497,7 +520,8 @@ * @param data * Data to digest * @return SHA-512 digest as a hex string - * @throws IOException On error reading from the stream + * @throws IOException + * On error reading from the stream * @since 1.4 */ public static String sha512Hex(InputStream data) throws IOException { @@ -536,7 +560,8 @@ * @param data * Data to digest * @return SHA-1 digest as a hex string - * @throws IOException On error reading from the stream + * @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=800270&r1=800269&r2=800270&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 Mon Aug 3 09:32:08 2009 @@ -21,6 +21,8 @@ import java.io.IOException; import java.util.Random; +import org.apache.commons.codec.binary.StringUtils; + import junit.framework.TestCase; /** @@ -33,6 +35,10 @@ private byte[] testData = new byte[1024*1024]; + private byte[] getBytesUtf8(String hashMe) { + return StringUtils.getBytesUtf8(hashMe); + } + /* (non-Javadoc) * @see junit.framework.TestCase#setUp() */ @@ -40,6 +46,10 @@ new Random().nextBytes(testData); } + public void testConstructable() { + new DigestUtils(); + } + public void testInternalNoSuchAlgorithmException() { try { DigestUtils.getDigest("Bogus Bogus"); @@ -72,17 +82,17 @@ assertEquals(DigestUtils.md5Hex(testData), DigestUtils.md5Hex(new ByteArrayInputStream(testData))); } - + /** * An MD5 hash converted to hex should always be 32 characters. */ public void testMD5HexLength() { String hashMe = "this is some string that is longer than 32 characters"; - String hash = DigestUtils.md5Hex(hashMe.getBytes()); + String hash = DigestUtils.md5Hex(getBytesUtf8(hashMe)); assertEquals(32, hash.length()); hashMe = "length < 32"; - hash = DigestUtils.md5Hex(hashMe.getBytes()); + hash = DigestUtils.md5Hex(getBytesUtf8(hashMe)); assertEquals(32, hash.length()); } @@ -91,11 +101,11 @@ */ public void testMD5Length() { String hashMe = "this is some string that is longer than 16 characters"; - byte[] hash = DigestUtils.md5(hashMe.getBytes()); + byte[] hash = DigestUtils.md5(getBytesUtf8(hashMe)); assertEquals(16, hash.length); hashMe = "length < 16"; - hash = DigestUtils.md5(hashMe.getBytes()); + hash = DigestUtils.md5(getBytesUtf8(hashMe)); assertEquals(16, hash.length); } @@ -104,13 +114,13 @@ assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", DigestUtils.sha256Hex("abc")); assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", - DigestUtils.sha256Hex("abc".getBytes())); + DigestUtils.sha256Hex(getBytesUtf8("abc"))); assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); assertEquals(DigestUtils.sha256Hex(testData), DigestUtils.sha256Hex(new ByteArrayInputStream(testData))); - } + } public void testSha384() throws IOException { // Examples from FIPS 180-2 @@ -119,14 +129,14 @@ DigestUtils.sha384Hex("abc")); assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + "8086072ba1e7cc2358baeca134c825a7", - DigestUtils.sha384Hex("abc".getBytes())); + DigestUtils.sha384Hex(getBytesUtf8("abc"))); assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" + "fcc7c71a557e2db966c3e9fa91746039", DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); assertEquals(DigestUtils.sha384Hex(testData), DigestUtils.sha384Hex(new ByteArrayInputStream(testData))); - } + } public void testSha512() throws IOException { // Examples from FIPS 180-2 @@ -135,20 +145,20 @@ DigestUtils.sha512Hex("abc")); assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", - DigestUtils.sha512Hex("abc".getBytes())); + DigestUtils.sha512Hex(getBytesUtf8("abc"))); assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); assertEquals(DigestUtils.sha512Hex(testData), DigestUtils.sha512Hex(new ByteArrayInputStream(testData))); -} +} public void testShaHex() throws IOException { // Examples from FIPS 180-1 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc")); - assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc".getBytes())); + assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex(getBytesUtf8("abc"))); assertEquals( "84983e441c3bd26ebaae4aa1f95129e5e54670f1", @@ -156,9 +166,5 @@ assertEquals(DigestUtils.shaHex(testData), DigestUtils.shaHex(new ByteArrayInputStream(testData))); - } - - public void testConstructable() { - new DigestUtils(); } }