Author: ggregory Date: Thu Apr 14 11:39:07 2005 New Revision: 161335 URL: http://svn.apache.org/viewcvs?view=rev&rev=161335 Log: [codec] Add methods for SHA-256, SHA-384, and SHA-512 to DigestUtils. http://issues.apache.org/bugzilla/show_bug.cgi?id=34157.
Modified: jakarta/commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java jakarta/commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java Modified: jakarta/commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java?view=diff&r1=161334&r2=161335 ============================================================================== --- jakarta/commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java (original) +++ jakarta/commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/DigestUtils.java Thu Apr 14 11:39:07 2005 @@ -1,5 +1,5 @@ /* - * Copyright 2001-2004 The Apache Software Foundation. + * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,7 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.apache.commons.codec.digest; @@ -22,19 +22,25 @@ import org.apache.commons.codec.binary.Hex; /** - * Operations to simplifiy common [EMAIL PROTECTED] java.security.MessageDigest} tasks. This - * class is thread safe. - * + * Operations to simplifiy common [EMAIL PROTECTED] java.security.MessageDigest} tasks. This class is thread safe. + * * @author Apache Software Foundation + * @version $Id$ */ public class DigestUtils { /** - * Returns a MessageDigest for the given <code>algorithm</code>. - * - * @param algorithm The MessageDigest algorithm name. + * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>. + * + * @param algorithm + * the name of the algorithm requested. See <a + * href="http://java.sun.com/j2se/1.3/docs/guide/security/CryptoSpec.html#AppA">Appendix A in the Java + * Cryptography Architecture API Specification & Reference</a> for information about standard algorithm + * names. * @return An MD5 digest instance. - * @throws RuntimeException when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught, + * @see MessageDigest#getInstance(String) + * @throws RuntimeException + * when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught. */ static MessageDigest getDigest(String algorithm) { try { @@ -46,29 +52,73 @@ /** * Returns an MD5 MessageDigest. - * + * * @return An MD5 digest instance. - * @throws RuntimeException when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught, + * @throws RuntimeException + * when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught. */ private static MessageDigest getMd5Digest() { return getDigest("MD5"); } /** - * Returns an SHA digest. - * - * @return An SHA digest instance. - * @throws RuntimeException when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught, + * Returns an SHA-256 digest. + * <p> + * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. + * </p> + * + * @return An SHA-256 digest instance. + * @throws RuntimeException + * when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught. + */ + private static MessageDigest getSha256Digest() { + return getDigest("SHA-256"); + } + + /** + * Returns an SHA-384 digest. + * <p> + * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. + * </p> + * + * @return An SHA-384 digest instance. + * @throws RuntimeException + * when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught. + */ + private static MessageDigest getSha384Digest() { + return getDigest("SHA-384"); + } + + /** + * Returns an SHA-512 digest. + * <p> + * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. + * </p> + * + * @return An SHA-512 digest instance. + * @throws RuntimeException + * when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught. + */ + private static MessageDigest getSha512Digest() { + return getDigest("SHA-512"); + } + + /** + * Returns an SHA-1 digest. + * + * @return An SHA-1 digest instance. + * @throws RuntimeException + * when a [EMAIL PROTECTED] java.security.NoSuchAlgorithmException} is caught. */ private static MessageDigest getShaDigest() { return getDigest("SHA"); } /** - * Calculates the MD5 digest and returns the value as a 16 element - * <code>byte[]</code>. - * - * @param data Data to digest + * 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(byte[] data) { @@ -76,10 +126,10 @@ } /** - * Calculates the MD5 digest and returns the value as a 16 element - * <code>byte[]</code>. - * - * @param data Data to digest + * 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) { @@ -87,10 +137,10 @@ } /** - * Calculates the MD5 digest and returns the value as a 32 character - * hex string. - * - * @param data Data to digest + * 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 */ public static String md5Hex(byte[] data) { @@ -98,10 +148,10 @@ } /** - * Calculates the MD5 digest and returns the value as a 32 character - * hex string. - * - * @param data Data to digest + * 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 */ public static String md5Hex(String data) { @@ -109,45 +159,215 @@ } /** - * Calculates the SHA digest and returns the value as a - * <code>byte[]</code>. - * - * @param data Data to digest - * @return SHA digest + * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. + * + * @param data + * Data to digest + * @return SHA-1 digest */ public static byte[] sha(byte[] data) { return getShaDigest().digest(data); } /** - * Calculates the SHA digest and returns the value as a - * <code>byte[]</code>. - * - * @param data Data to digest - * @return SHA digest + * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. + * + * @param data + * Data to digest + * @return SHA-1 digest */ public static byte[] sha(String data) { return sha(data.getBytes()); } /** - * Calculates the SHA digest and returns the value as a hex string. - * - * @param data Data to digest - * @return SHA digest as a hex string + * 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 + */ + public static byte[] sha256(byte[] data) { + return getSha256Digest().digest(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 + */ + public static byte[] sha256(String data) { + return sha256(data.getBytes()); + } + + /** + * 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 + */ + public static String sha256Hex(byte[] data) { + return new String(Hex.encodeHex(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 + */ + public static String sha256Hex(String data) { + 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. + * </p> + * + * @param data + * Data to digest + * @return SHA-384 digest + */ + public static byte[] sha384(byte[] data) { + // FIXME: check Sun docs for how to get a sha 384 digest + return getSha384Digest().digest(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 + */ + public static byte[] sha384(String data) { + return sha384(data.getBytes()); + } + + /** + * 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 + */ + public static String sha384Hex(byte[] data) { + return new String(Hex.encodeHex(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 + */ + public static String sha384Hex(String data) { + 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. + * </p> + * + * @param data + * Data to digest + * @return SHA-512 digest + */ + public static byte[] sha512(byte[] data) { + return getSha512Digest().digest(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 + */ + public static byte[] sha512(String data) { + return sha512(data.getBytes()); + } + + /** + * 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 + */ + public static String sha512Hex(byte[] data) { + return new String(Hex.encodeHex(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 + */ + public static String sha512Hex(String data) { + return new String(Hex.encodeHex(sha512(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 */ public static String shaHex(byte[] data) { return new String(Hex.encodeHex(sha(data))); } /** - * Calculates the SHA digest and returns the value as a hex string. - * - * @param data Data to digest - * @return SHA digest as a hex string + * 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 */ public static String shaHex(String data) { return new String(Hex.encodeHex(sha(data))); } - } Modified: jakarta/commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java?view=diff&r1=161334&r2=161335 ============================================================================== --- jakarta/commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java (original) +++ jakarta/commons/proper/codec/trunk/src/test/org/apache/commons/codec/digest/DigestUtilsTest.java Thu Apr 14 11:39:07 2005 @@ -19,9 +19,10 @@ import junit.framework.TestCase; /** - * Tests Digest methods. + * Tests DigestUtils methods. * * @author Apache Software Foundation + * @version $Id$ */ public class DigestUtilsTest extends TestCase { @@ -81,6 +82,44 @@ assertEquals(16, hash.length); } + public void testSha256() { + // Examples from FIPS 180-2 + assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + DigestUtils.sha256Hex("abc")); + assertEquals("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + DigestUtils.sha256Hex("abc".getBytes())); + assertEquals("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", + DigestUtils.sha256Hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); + } + + public void testSha384() { + // Examples from FIPS 180-2 + assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + + "8086072ba1e7cc2358baeca134c825a7", + DigestUtils.sha384Hex("abc")); + assertEquals("cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed" + + "8086072ba1e7cc2358baeca134c825a7", + DigestUtils.sha384Hex("abc".getBytes())); + assertEquals("09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712" + + "fcc7c71a557e2db966c3e9fa91746039", + DigestUtils.sha384Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); + } + + public void testSha512() { + // Examples from FIPS 180-2 + assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", + DigestUtils.sha512Hex("abc")); + assertEquals("ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" + + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", + DigestUtils.sha512Hex("abc".getBytes())); + assertEquals("8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018" + + "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", + DigestUtils.sha512Hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")); + } + public void testShaHex() { // Examples from FIPS 180-1 assertEquals("a9993e364706816aba3e25717850c26c9cd0d89d", DigestUtils.shaHex("abc")); @@ -90,6 +129,5 @@ assertEquals( "84983e441c3bd26ebaae4aa1f95129e5e54670f1", DigestUtils.shaHex("abcdbcdecdefdefgefghfghighij" + "hijkijkljklmklmnlmnomnopnopq")); - } - + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]