Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5A39E2009C5 for ; Mon, 16 May 2016 22:48:05 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 58D3C160A19; Mon, 16 May 2016 20:48:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 208AB160A16 for ; Mon, 16 May 2016 22:48:02 +0200 (CEST) Received: (qmail 98255 invoked by uid 500); 16 May 2016 20:47:57 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 98079 invoked by uid 99); 16 May 2016 20:47:57 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 May 2016 20:47:57 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id B1AF0C06CB for ; Mon, 16 May 2016 20:47:56 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 2.049 X-Spam-Level: ** X-Spam-Status: No, score=2.049 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, KAM_LOTSOFHASH=0.25, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id g1yy2wl4lIuq for ; Mon, 16 May 2016 20:47:49 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with ESMTP id ED6FA5F24C for ; Mon, 16 May 2016 20:47:48 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id E28A5E006A for ; Mon, 16 May 2016 20:47:47 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id D6C603A019D for ; Mon, 16 May 2016 20:47:47 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1744132 - in /commons/proper/codec/trunk/src: changes/ main/java/org/apache/commons/codec/digest/ test/java/org/apache/commons/codec/digest/ Date: Mon, 16 May 2016 20:47:47 -0000 To: commits@commons.apache.org From: ggregory@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160516204747.D6C603A019D@svn01-us-west.apache.org> archived-at: Mon, 16 May 2016 20:48:05 -0000 Author: ggregory Date: Mon May 16 20:47:47 2016 New Revision: 1744132 URL: http://svn.apache.org/viewvc?rev=1744132&view=rev Log: [CODEC-218] Refactor HmacUtils methods into the HmacAlgorithms enum. Added: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java Modified: commons/proper/codec/trunk/src/changes/changes.xml commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacAlgorithms.java commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacUtilsTest.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=1744132&r1=1744131&r2=1744132&view=diff ============================================================================== --- commons/proper/codec/trunk/src/changes/changes.xml (original) +++ commons/proper/codec/trunk/src/changes/changes.xml Mon May 16 20:47:47 2016 @@ -47,6 +47,7 @@ The type attribute can be add,u Charsets Javadoc breaks build when using Java 8 Bug in HW rule in Soundex Javadoc for SHA-224 DigestUtils methods should mention Java 1.8.0 restriction instead of 1.4.0. + Refactor HmacUtils methods into the HmacAlgorithms enum Add HmacAlgorithms.HMAC_SHA_224 (Java 8 only) Support JEP 287: SHA-3 Hash Algorithms Create a minimal Digest command line utility: org.apache.commons.codec.digest.Digest Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacAlgorithms.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacAlgorithms.java?rev=1744132&r1=1744131&r2=1744132&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacAlgorithms.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacAlgorithms.java Mon May 16 20:47:47 2016 @@ -17,6 +17,16 @@ package org.apache.commons.codec.digest; +import java.io.IOException; +import java.io.InputStream; +import java.security.Key; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.Mac; + +import org.apache.commons.codec.binary.Hex; +import org.apache.commons.codec.binary.StringUtils; + /** * Standard {@link HmacUtils} algorithm names from the Java Cryptography Architecture Standard Algorithm Name * Documentation. @@ -25,8 +35,12 @@ package org.apache.commons.codec.digest; * Note: Not all JCE implementations support all the algorithms in this enum. *

* - * @see Java Cryptography - * Architecture Standard Algorithm Name Documentation + * @see Java + * 6 Cryptography Architecture Sun Providers Documentation + * @see Java + * 7 Cryptography Architecture Sun Providers Documentation + * @see Java + * 8 Cryptography Architecture Sun Providers Documentation * @since 1.10 * @version $Id$ */ @@ -51,7 +65,7 @@ public enum HmacAlgorithms { /** * The HmacSHA256 Message Authentication Code (MAC) algorithm specified in RFC 2104 and FIPS PUB 180-2. *

- * Every implementation of the Java 8 platform is required to support this standard MAC algorithm. + * Every implementation of the Java platform is required to support this standard MAC algorithm. *

*/ HMAC_SHA_224("HmacSHA224"), @@ -80,23 +94,181 @@ public enum HmacAlgorithms { */ HMAC_SHA_512("HmacSHA512"); - private final String algorithm; + private final String name; private HmacAlgorithms(final String algorithm) { - this.algorithm = algorithm; + this.name = algorithm; + } + + /** + * Returns an initialized Mac for the this algorithm. + *

+ * Every implementation of the Java platform is required to support this standard Mac algorithm. + *

+ * + * @param key + * They key for the keyed digest (must not be null) + * @return A Mac instance initialized with the given key. + * @see Mac#getInstance(String) + * @see Mac#init(Key) + * @throws IllegalArgumentException + * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + */ + public Mac getHmac(final byte[] key) { + return HmacUtils.getInitializedMac(name, key); + } + + /** + * Returns an initialized Mac for this algorithm. + * + * @param key + * They key for the keyed digest (must not be null) + * @return A Mac instance initialized with the given key. + * @see Mac#getInstance(String) + * @see Mac#init(Key) + * @throws IllegalArgumentException + * when key is null or invalid. + */ + public Mac getInitializedMac(final byte[] key) { + return HmacUtils.getInitializedMac(name, key); + } + + /** + * Gets the algorithm name. + * + * @return the algorithm name. + */ + public String getName() { + return name; + } + + /** + * Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value. + * + * @param key + * They key for the keyed digest (must not be null) + * @param valueToDigest + * The value (data) which should to digest (maybe empty or null) + * @return HMAC for the given key and value + * @throws IllegalArgumentException + * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + */ + public byte[] hmac(final byte[] key, final byte[] valueToDigest) { + try { + return getHmac(key).doFinal(valueToDigest); + } catch (final IllegalStateException e) { + // cannot happen + throw new IllegalArgumentException(e); + } + } + + /** + * Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value. + * + * @param key + * They key for the keyed digest (must not be null) + * @param valueToDigest + * The value (data) which should to digest. The InputStream must not be null and will not be closed. + * @return HMAC for the given key and value + * @throws IOException + * If an I/O error occurs. + * @throws IllegalArgumentException + * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + */ + public byte[] hmac(final byte[] key, final InputStream valueToDigest) throws IOException { + return HmacUtils.updateHmac(getHmac(key), valueToDigest).doFinal(); + } + + /** + * Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value. + * + * @param key + * They key for the keyed digest (must not be null) + * @param valueToDigest + * The value (data) which should to digest (maybe empty or null) + * @return HMAC for the given key and value + * @throws IllegalArgumentException + * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + */ + public byte[] hmac(final String key, final String valueToDigest) { + return hmac(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); + } + + /** + * Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value. + * + * @param key + * They key for the keyed digest (must not be null) + * @param valueToDigest + * The value (data) which should to digest (maybe empty or null) + * @return HMAC for the given key and value as a hex string (lowercase) + * @throws IllegalArgumentException + * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + */ + public String hmacHex(final byte[] key, final byte[] valueToDigest) { + return Hex.encodeHexString(hmac(key, valueToDigest)); + } + + /** + * Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value. + * + * @param key + * They key for the keyed digest (must not be null) + * @param valueToDigest + * The value (data) which should to digest. The InputStream must not be null and will not be closed. + * @return HMAC for the given key and value as a hex string (lowercase) + * @throws IOException + * If an I/O error occurs. + * @throws IllegalArgumentException + * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + */ + public String hmacHex(final byte[] key, final InputStream valueToDigest) throws IOException { + return Hex.encodeHexString(hmac(key, valueToDigest)); + } + + /** + * Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value. + * + * @param key + * They key for the keyed digest (must not be null) + * @param valueToDigest + * The value (data) which should to digest (maybe empty or null) + * @return HMAC for the given key and value as a hex string (lowercase) + * @throws IllegalArgumentException + * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + */ + public String hmacHex(final String key, final String valueToDigest) { + return Hex.encodeHexString(hmac(key, valueToDigest)); + } + + /** + * Returns whether this algorithm is available + * + * @return whether this algorithm is available + */ + public boolean isAvailable() { + try { + Mac.getInstance(name); + return true; + } catch (NoSuchAlgorithmException e) { + return false; + } } /** * The algorithm name * - * @see Java - * Cryptography Architecture Sun Providers Documentation + * @see + * Java 6 Cryptography Architecture Sun Providers Documentation + * @see + * Java 7 Cryptography Architecture Sun Providers Documentation + * @see + * Java 8 Cryptography Architecture Sun Providers Documentation * @return The algorithm name ("HmacSHA512" for example) */ @Override public String toString() { - return algorithm; + return name; } - + } Modified: commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java?rev=1744132&r1=1744131&r2=1744132&view=diff ============================================================================== --- commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java (original) +++ commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacUtils.java Mon May 16 20:47:47 2016 @@ -26,7 +26,6 @@ import java.security.NoSuchAlgorithmExce import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; -import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.binary.StringUtils; /** @@ -57,9 +56,11 @@ public final class HmacUtils { * @see Mac#init(Key) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.HMAC_MD5.getHmac(byte[])} */ + @Deprecated public static Mac getHmacMd5(final byte[] key) { - return getInitializedMac(HmacAlgorithms.HMAC_MD5, key); + return HmacAlgorithms.HMAC_MD5.getHmac(key); } /** @@ -75,9 +76,11 @@ public final class HmacUtils { * @see Mac#init(Key) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.HMAC_SHA1.getHmac(byte[])} */ + @Deprecated public static Mac getHmacSha1(final byte[] key) { - return getInitializedMac(HmacAlgorithms.HMAC_SHA_1, key); + return HmacAlgorithms.HMAC_SHA_1.getHmac(key); } /** @@ -93,9 +96,11 @@ public final class HmacUtils { * @see Mac#init(Key) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.HMAC_SHA256.getHmac(byte[])} */ + @Deprecated public static Mac getHmacSha256(final byte[] key) { - return getInitializedMac(HmacAlgorithms.HMAC_SHA_256, key); + return HmacAlgorithms.HMAC_SHA_256.getHmac(key); } /** @@ -111,9 +116,11 @@ public final class HmacUtils { * @see Mac#init(Key) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.HMAC_SHA384.getHmac(byte[])} */ + @Deprecated public static Mac getHmacSha384(final byte[] key) { - return getInitializedMac(HmacAlgorithms.HMAC_SHA_384, key); + return HmacAlgorithms.HMAC_SHA_384.getHmac(key); } /** @@ -129,19 +136,21 @@ public final class HmacUtils { * @see Mac#init(Key) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.HMAC_SHA512.getHmac(byte[])} */ + @Deprecated public static Mac getHmacSha512(final byte[] key) { - return getInitializedMac(HmacAlgorithms.HMAC_SHA_512, key); + return HmacAlgorithms.HMAC_SHA_512.getHmac(key); } /** * Returns an initialized Mac for the given algorithm. * * @param algorithm - * the name of the algorithm requested. See Appendix - * A in the Java Cryptography Architecture Reference Guide for information about standard algorithm - * names. + * the name of the algorithm requested. See + * Appendix A in the Java Cryptography Architecture Reference Guide for information about standard + * algorithm names. * @param key * They key for the keyed digest (must not be null) * @return A Mac instance initialized with the given key. @@ -149,19 +158,21 @@ public final class HmacUtils { * @see Mac#init(Key) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@link HmacAlgorithms#getInitializedMac(byte[])}. */ + @Deprecated public static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key) { - return getInitializedMac(algorithm.toString(), key); + return algorithm.getInitializedMac(key); } /** * Returns an initialized Mac for the given algorithm. * * @param algorithm - * the name of the algorithm requested. See Appendix - * A in the Java Cryptography Architecture Reference Guide for information about standard algorithm - * names. + * the name of the algorithm requested. See + * Appendix A in the Java Cryptography Architecture Reference Guide for information about standard + * algorithm names. * @param key * They key for the keyed digest (must not be null) * @return A Mac instance initialized with the given key. @@ -200,14 +211,11 @@ public final class HmacUtils { * @return HmacMD5 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.MD5.getHmac(byte[], byte[])} */ + @Deprecated public static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest) { - try { - return getHmacMd5(key).doFinal(valueToDigest); - } catch (final IllegalStateException e) { - // cannot happen - throw new IllegalArgumentException(e); - } + return HmacAlgorithms.HMAC_MD5.hmac(key, valueToDigest); } /** @@ -225,9 +233,11 @@ public final class HmacUtils { * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.MD5.getHmac(byte[], InputStream)} */ + @Deprecated public static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest) throws IOException { - return updateHmac(getHmacMd5(key), valueToDigest).doFinal(); + return HmacAlgorithms.HMAC_MD5.hmac(key, valueToDigest); } /** @@ -240,9 +250,11 @@ public final class HmacUtils { * @return HmacMD5 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.MD5.getHmac(String, String)} */ + @Deprecated public static byte[] hmacMd5(final String key, final String valueToDigest) { - return hmacMd5(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); + return HmacAlgorithms.HMAC_MD5.hmac(key, valueToDigest); } /** @@ -255,9 +267,11 @@ public final class HmacUtils { * @return HmacMD5 MAC for the given key and value as a hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.MD5.getHmacHex(byte[], byte[])} */ + @Deprecated public static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest) { - return Hex.encodeHexString(hmacMd5(key, valueToDigest)); + return HmacAlgorithms.HMAC_MD5.hmacHex(key, valueToDigest); } /** @@ -275,9 +289,11 @@ public final class HmacUtils { * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.MD5.getHmacHex(byte[], InputStream)} */ + @Deprecated public static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest) throws IOException { - return Hex.encodeHexString(hmacMd5(key, valueToDigest)); + return HmacAlgorithms.HMAC_MD5.hmacHex(key, valueToDigest); } /** @@ -290,9 +306,11 @@ public final class HmacUtils { * @return HmacMD5 MAC for the given key and value as a hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.MD5.getHmacHex(String, String)} */ + @Deprecated public static String hmacMd5Hex(final String key, final String valueToDigest) { - return Hex.encodeHexString(hmacMd5(key, valueToDigest)); + return HmacAlgorithms.HMAC_MD5.hmacHex(key, valueToDigest); } // hmacSha1 @@ -307,14 +325,11 @@ public final class HmacUtils { * @return HmacSHA1 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_1.getHmac(byte[], byte[])} */ + @Deprecated public static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest) { - try { - return getHmacSha1(key).doFinal(valueToDigest); - } catch (final IllegalStateException e) { - // cannot happen - throw new IllegalArgumentException(e); - } + return HmacAlgorithms.HMAC_SHA_1.hmac(key, valueToDigest); } /** @@ -332,9 +347,11 @@ public final class HmacUtils { * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_1.getHmac(byte[], InputStream)} */ + @Deprecated public static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest) throws IOException { - return updateHmac(getHmacSha1(key), valueToDigest).doFinal(); + return HmacAlgorithms.HMAC_SHA_1.hmac(key, valueToDigest); } /** @@ -347,9 +364,11 @@ public final class HmacUtils { * @return HmacSHA1 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_1.getHmac(String, String)} */ + @Deprecated public static byte[] hmacSha1(final String key, final String valueToDigest) { - return hmacSha1(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); + return HmacAlgorithms.HMAC_SHA_1.hmac(key, valueToDigest); } /** @@ -362,9 +381,11 @@ public final class HmacUtils { * @return HmacSHA1 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_1.getHmacHex(byte[], byte[])} */ + @Deprecated public static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest) { - return Hex.encodeHexString(hmacSha1(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_1.hmacHex(key, valueToDigest); } /** @@ -382,9 +403,11 @@ public final class HmacUtils { * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_1.getHmacHex(byte[], InputStream)} */ + @Deprecated public static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest) throws IOException { - return Hex.encodeHexString(hmacSha1(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_1.hmacHex(key, valueToDigest); } /** @@ -397,9 +420,11 @@ public final class HmacUtils { * @return HmacSHA1 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_1.getHmacHex(String, String)} */ + @Deprecated public static String hmacSha1Hex(final String key, final String valueToDigest) { - return Hex.encodeHexString(hmacSha1(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_1.hmacHex(key, valueToDigest); } // hmacSha256 @@ -414,14 +439,11 @@ public final class HmacUtils { * @return HmacSHA256 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_256.getHmac(byte[], byte[])} */ + @Deprecated public static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest) { - try { - return getHmacSha256(key).doFinal(valueToDigest); - } catch (final IllegalStateException e) { - // cannot happen - throw new IllegalArgumentException(e); - } + return HmacAlgorithms.HMAC_SHA_256.hmac(key, valueToDigest); } /** @@ -437,11 +459,13 @@ public final class HmacUtils { * @return HmacSHA256 MAC for the given key and value * @throws IOException * If an I/O error occurs. -s * @throws IllegalArgumentException + * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_256.getHmac(byte[], InputStream)} */ + @Deprecated public static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest) throws IOException { - return updateHmac(getHmacSha256(key), valueToDigest).doFinal(); + return HmacAlgorithms.HMAC_SHA_256.hmac(key, valueToDigest); } /** @@ -454,9 +478,11 @@ s * @throws IllegalArgumentException * @return HmacSHA256 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_256.getHmac(String, String)} */ + @Deprecated public static byte[] hmacSha256(final String key, final String valueToDigest) { - return hmacSha256(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); + return HmacAlgorithms.HMAC_SHA_256.hmac(key, valueToDigest); } /** @@ -469,9 +495,11 @@ s * @throws IllegalArgumentException * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_256.getHmacHex(byte[], byte[])} */ + @Deprecated public static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest) { - return Hex.encodeHexString(hmacSha256(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_256.hmacHex(key, valueToDigest); } /** @@ -489,9 +517,11 @@ s * @throws IllegalArgumentException * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_256.getHmacHex(byte[], InputStream)} */ + @Deprecated public static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest) throws IOException { - return Hex.encodeHexString(hmacSha256(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_256.hmacHex(key, valueToDigest); } /** @@ -504,9 +534,11 @@ s * @throws IllegalArgumentException * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_256.getHmacHex(String, String)} */ + @Deprecated public static String hmacSha256Hex(final String key, final String valueToDigest) { - return Hex.encodeHexString(hmacSha256(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_256.hmacHex(key, valueToDigest); } // hmacSha384 @@ -521,14 +553,11 @@ s * @throws IllegalArgumentException * @return HmacSHA384 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_384.getHmac(byte[], byte[])} */ + @Deprecated public static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest) { - try { - return getHmacSha384(key).doFinal(valueToDigest); - } catch (final IllegalStateException e) { - // cannot happen - throw new IllegalArgumentException(e); - } + return HmacAlgorithms.HMAC_SHA_384.hmac(key, valueToDigest); } /** @@ -546,9 +575,11 @@ s * @throws IllegalArgumentException * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_384.getHmac(byte[], InputStream)} */ + @Deprecated public static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest) throws IOException { - return updateHmac(getHmacSha384(key), valueToDigest).doFinal(); + return HmacAlgorithms.HMAC_SHA_384.hmac(key, valueToDigest); } /** @@ -561,9 +592,11 @@ s * @throws IllegalArgumentException * @return HmacSHA384 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_384.getHmac(String, String)} */ + @Deprecated public static byte[] hmacSha384(final String key, final String valueToDigest) { - return hmacSha384(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); + return HmacAlgorithms.HMAC_SHA_384.hmac(key, valueToDigest); } /** @@ -576,9 +609,11 @@ s * @throws IllegalArgumentException * @return HmacSHA384 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_384.getHmacHex(byte[], byte[])} */ + @Deprecated public static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest) { - return Hex.encodeHexString(hmacSha384(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_384.hmacHex(key, valueToDigest); } /** @@ -596,9 +631,11 @@ s * @throws IllegalArgumentException * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_384.getHmacHex(byte[], InputStream)} */ + @Deprecated public static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest) throws IOException { - return Hex.encodeHexString(hmacSha384(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_384.hmacHex(key, valueToDigest); } /** @@ -611,9 +648,11 @@ s * @throws IllegalArgumentException * @return HmacSHA384 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_384.getHmacHex(String, String)} */ + @Deprecated public static String hmacSha384Hex(final String key, final String valueToDigest) { - return Hex.encodeHexString(hmacSha384(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_384.hmacHex(key, valueToDigest); } // hmacSha512 @@ -628,14 +667,11 @@ s * @throws IllegalArgumentException * @return HmacSHA512 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_512.getHmac(byte[], byte[])} */ + @Deprecated public static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest) { - try { - return getHmacSha512(key).doFinal(valueToDigest); - } catch (final IllegalStateException e) { - // cannot happen - throw new IllegalArgumentException(e); - } + return HmacAlgorithms.HMAC_SHA_512.hmac(key, valueToDigest); } /** @@ -653,9 +689,11 @@ s * @throws IllegalArgumentException * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_512.getHmac(byte[], InputStream)} */ + @Deprecated public static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest) throws IOException { - return updateHmac(getHmacSha512(key), valueToDigest).doFinal(); + return HmacAlgorithms.HMAC_SHA_512.hmac(key, valueToDigest); } /** @@ -668,9 +706,11 @@ s * @throws IllegalArgumentException * @return HmacSHA512 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_512.getHmac(String, String)} */ + @Deprecated public static byte[] hmacSha512(final String key, final String valueToDigest) { - return hmacSha512(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); + return HmacAlgorithms.HMAC_SHA_512.hmac(key, valueToDigest); } /** @@ -683,9 +723,11 @@ s * @throws IllegalArgumentException * @return HmacSHA512 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_512.getHmacHex(byte[], byte[])} */ + @Deprecated public static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest) { - return Hex.encodeHexString(hmacSha512(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_512.hmacHex(key, valueToDigest); } /** @@ -703,9 +745,11 @@ s * @throws IllegalArgumentException * If an I/O error occurs. * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_512.getHmacHex(byte[], InputStream)} */ + @Deprecated public static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest) throws IOException { - return Hex.encodeHexString(hmacSha512(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_512.hmacHex(key, valueToDigest); } /** @@ -718,9 +762,11 @@ s * @throws IllegalArgumentException * @return HmacSHA512 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. + * @deprecated Use {@code HmacAlgorithms.SHA_512.getHmacHex(String, String)} */ + @Deprecated public static String hmacSha512Hex(final String key, final String valueToDigest) { - return Hex.encodeHexString(hmacSha512(key, valueToDigest)); + return HmacAlgorithms.HMAC_SHA_512.hmacHex(key, valueToDigest); } // update Added: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java?rev=1744132&view=auto ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java (added) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacAlgorithmsTest.java Mon May 16 20:47:47 2016 @@ -0,0 +1,219 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * 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; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Collection; + +import javax.crypto.Mac; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Tests {@link MessageDigestAlgorithm}. + * + * @since 1.11 + */ +@RunWith(Parameterized.class) +public class HmacAlgorithmsTest { + + static final String STANDARD_KEY_STRING = "key"; + + static final byte[] STANDARD_KEY_BYTES = STANDARD_KEY_STRING.getBytes(); + + static final byte[] STANDARD_MD5_RESULT_BYTES = new byte[] { -128, 7, 7, 19, 70, 62, 119, 73, -71, 12, 45, -62, 73, + 17, -30, 117 }; + + static final String STANDARD_MD5_RESULT_STRING = "80070713463e7749b90c2dc24911e275"; + + static final String STANDARD_PHRASE_STRING = "The quick brown fox jumps over the lazy dog"; + + static final byte[] STANDARD_PHRASE_BYTES = STANDARD_PHRASE_STRING.getBytes(); + + static final byte[] STANDARD_SHA1_RESULT_BYTES = new byte[] { -34, 124, -101, -123, -72, -73, -118, -90, -68, -118, + 122, 54, -9, 10, -112, 112, 28, -99, -76, -39 }; + + static final String STANDARD_SHA1_RESULT_STRING = "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"; + + static final byte[] STANDARD_SHA256_RESULT_BYTES = new byte[] { -9, -68, -125, -12, 48, 83, -124, 36, -79, 50, -104, + -26, -86, 111, -79, 67, -17, 77, 89, -95, 73, 70, 23, 89, -105, 71, -99, -68, 45, 26, 60, -40 }; + + static final String STANDARD_SHA256_RESULT_STRING = "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"; + + static final byte[] STANDARD_SHA384_RESULT_BYTES = new byte[] { -41, -12, 114, 126, 44, 11, 57, -82, 15, 30, 64, + -52, -106, -10, 2, 66, -43, -73, -128, 24, 65, -50, -90, -4, 89, 44, 93, 62, 26, -27, 7, 0, 88, 42, -106, + -49, 53, -31, -27, 84, -103, 95, -28, -32, 51, -127, -62, 55 }; + + static final String STANDARD_SHA384_RESULT_STRING = "D7F4727E2C0B39AE0F1E40CC96F60242D5B7801841CEA6FC592C5D3E1AE50700582A96CF35E1E554995FE4E03381C237" + .toLowerCase(); + + static final byte[] STANDARD_SHA512_RESULT_BYTES = new byte[] { -76, 42, -16, -112, 87, -70, -63, -30, -44, 23, 8, + -28, -118, -112, 46, 9, -75, -1, 127, 18, -85, 66, -118, 79, -24, 102, 83, -57, 61, -46, 72, -5, -126, -7, + 72, -91, 73, -9, -73, -111, -91, -76, 25, 21, -18, 77, 30, -61, -109, 83, 87, -28, -30, 49, 114, 80, -48, + 55, 42, -6, 46, -66, -21, 58 }; + + static final String STANDARD_SHA512_RESULT_STRING = "B42AF09057BAC1E2D41708E48A902E09B5FF7F12AB428A4FE86653C73DD248FB82F948A549F7B791A5B41915EE4D1EC3935357E4E2317250D0372AFA2EBEEB3A" + .toLowerCase(); + + private static final byte[] EMPTY_BYTE_ARRAY = new byte[] {}; + + // TODO HMAC_SHA_224 + @Parameters(name = "{0}") + public static Collection data() { + return Arrays.asList( + new Object[][] { { HmacAlgorithms.HMAC_MD5, STANDARD_MD5_RESULT_BYTES, STANDARD_MD5_RESULT_STRING }, + { HmacAlgorithms.HMAC_SHA_1, STANDARD_SHA1_RESULT_BYTES, STANDARD_SHA1_RESULT_STRING }, + { HmacAlgorithms.HMAC_SHA_256, STANDARD_SHA256_RESULT_BYTES, STANDARD_SHA256_RESULT_STRING }, + { HmacAlgorithms.HMAC_SHA_384, STANDARD_SHA384_RESULT_BYTES, STANDARD_SHA384_RESULT_STRING }, + { HmacAlgorithms.HMAC_SHA_512, STANDARD_SHA512_RESULT_BYTES, STANDARD_SHA512_RESULT_STRING } }); + } + + private DigestUtilsTest digestUtilsTest; + + private final HmacAlgorithms hmacAlgorithm; + + private final byte[] standardResultBytes; + private final String standardResultString; + + public HmacAlgorithmsTest(HmacAlgorithms hmacAlgorithm, byte[] standardResultBytes, String standardResultString) { + Assume.assumeTrue(hmacAlgorithm.isAvailable()); + this.hmacAlgorithm = hmacAlgorithm; + this.standardResultBytes = standardResultBytes; + this.standardResultString = standardResultString; + } + + private byte[] getTestData() { + return digestUtilsTest.getTestData(); + } + + private File getTestFile() { + return digestUtilsTest.getTestFile(); + } + + @Before + public void setUp() throws Exception { + digestUtilsTest = new DigestUtilsTest(); + digestUtilsTest.setUp(); + } + + @After + public void tearDown() throws Exception { + digestUtilsTest.tearDown(); + digestUtilsTest = null; + } + + @Test + public void testAlgorithm() throws IOException, NoSuchAlgorithmException { + final String algorithm = hmacAlgorithm.getName(); + Assert.assertNotNull(algorithm); + Assert.assertFalse(algorithm.isEmpty()); + Assume.assumeTrue(hmacAlgorithm.isAvailable()); + Mac.getInstance(algorithm); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetHmacEmptyKey() { + hmacAlgorithm.getHmac(EMPTY_BYTE_ARRAY); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetHmacNullKey() { + hmacAlgorithm.getHmac(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testHmacFailByteArray() throws IOException { + hmacAlgorithm.hmac((byte[]) null, STANDARD_PHRASE_BYTES); + } + + @Test(expected = IllegalArgumentException.class) + public void testHmacFailInputStream() throws IOException { + hmacAlgorithm.hmac((byte[]) null, new ByteArrayInputStream(STANDARD_PHRASE_BYTES)); + } + + @Test(expected = IllegalArgumentException.class) + public void testHmacFailString() throws IOException { + hmacAlgorithm.hmac((String) null, STANDARD_PHRASE_STRING); + } + + @Test(expected = IllegalArgumentException.class) + public void testHmacHexFailByteArray() throws IOException { + hmacAlgorithm.hmacHex((byte[]) null, STANDARD_PHRASE_BYTES); + } + + @Test(expected = IllegalArgumentException.class) + public void testHmacHexFailInputStream() throws IOException { + hmacAlgorithm.hmacHex((byte[]) null, new ByteArrayInputStream(STANDARD_PHRASE_BYTES)); + } + + @Test(expected = IllegalArgumentException.class) + public void testHmacHexFailString() throws IOException { + hmacAlgorithm.hmacHex((String) null, STANDARD_PHRASE_STRING); + } + + @Test + public void testInitializedMac() throws IOException { + final Mac mac = HmacUtils.getInitializedMac(hmacAlgorithm, STANDARD_KEY_BYTES); + final Mac mac2 = HmacUtils.getInitializedMac(hmacAlgorithm.getName(), STANDARD_KEY_BYTES); + Assert.assertArrayEquals(standardResultBytes, HmacUtils.updateHmac(mac, STANDARD_PHRASE_STRING).doFinal()); + Assert.assertArrayEquals(standardResultBytes, HmacUtils.updateHmac(mac2, STANDARD_PHRASE_STRING).doFinal()); + } + + @Test + public void testMacByteArary() throws IOException { + Assert.assertArrayEquals(standardResultBytes, hmacAlgorithm.hmac(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); + } + + @Test + public void testMacHexByteArray() throws IOException { + Assert.assertEquals(standardResultString, hmacAlgorithm.hmacHex(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); + } + + @Test + public void testMacHexInputStream() throws IOException { + Assert.assertEquals(standardResultString, + hmacAlgorithm.hmacHex(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); + } + + @Test + public void testMacHexString() throws IOException { + Assert.assertEquals(standardResultString, hmacAlgorithm.hmacHex(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); + } + + @Test + public void testMacInputStream() throws IOException { + Assert.assertArrayEquals(standardResultBytes, + hmacAlgorithm.hmac(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); + } + + @Test + public void testMacString() throws IOException { + Assert.assertArrayEquals(standardResultBytes, hmacAlgorithm.hmac(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); + } + +} Modified: commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacUtilsTest.java?rev=1744132&r1=1744131&r2=1744132&view=diff ============================================================================== --- commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacUtilsTest.java (original) +++ commons/proper/codec/trunk/src/test/java/org/apache/commons/codec/digest/HmacUtilsTest.java Mon May 16 20:47:47 2016 @@ -36,31 +36,6 @@ import org.junit.Test; * @version $Id$ */ public class HmacUtilsTest { - private static final String STANDARD_KEY_STRING = "key"; - private static final byte[] STANDARD_KEY_BYTES = STANDARD_KEY_STRING.getBytes(); - private static final byte[] STANDARD_MD5_RESULT_BYTES = new byte[] { -128, 7, 7, 19, 70, 62, 119, 73, -71, 12, 45, - -62, 73, 17, -30, 117 }; - private static final String STANDARD_MD5_RESULT_STRING = "80070713463e7749b90c2dc24911e275"; - private static final String STANDARD_PHRASE_STRING = "The quick brown fox jumps over the lazy dog"; - private static final byte[] STANDARD_PHRASE_BYTES = STANDARD_PHRASE_STRING.getBytes(); - private static final byte[] STANDARD_SHA1_RESULT_BYTES = new byte[] { -34, 124, -101, -123, -72, -73, -118, -90, - -68, -118, 122, 54, -9, 10, -112, 112, 28, -99, -76, -39 }; - private static final String STANDARD_SHA1_RESULT_STRING = "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"; - private static final byte[] STANDARD_SHA256_RESULT_BYTES = new byte[] { -9, -68, -125, -12, 48, 83, -124, 36, -79, - 50, -104, -26, -86, 111, -79, 67, -17, 77, 89, -95, 73, 70, 23, 89, -105, 71, -99, -68, 45, 26, 60, -40 }; - private static final String STANDARD_SHA256_RESULT_STRING = "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"; - private static final byte[] STANDARD_SHA384_RESULT_BYTES = new byte[] { -41, -12, 114, 126, 44, 11, 57, -82, 15, - 30, 64, -52, -106, -10, 2, 66, -43, -73, -128, 24, 65, -50, -90, -4, 89, 44, 93, 62, 26, -27, 7, 0, 88, 42, - -106, -49, 53, -31, -27, 84, -103, 95, -28, -32, 51, -127, -62, 55 }; - private static final String STANDARD_SHA384_RESULT_STRING = "D7F4727E2C0B39AE0F1E40CC96F60242D5B7801841CEA6FC592C5D3E1AE50700582A96CF35E1E554995FE4E03381C237" - .toLowerCase(); - private static final byte[] STANDARD_SHA512_RESULT_BYTES = new byte[] { -76, 42, -16, -112, 87, -70, -63, -30, -44, - 23, 8, -28, -118, -112, 46, 9, -75, -1, 127, 18, -85, 66, -118, 79, -24, 102, 83, -57, 61, -46, 72, -5, - -126, -7, 72, -91, 73, -9, -73, -111, -91, -76, 25, 21, -18, 77, 30, -61, -109, 83, 87, -28, -30, 49, 114, - 80, -48, 55, 42, -6, 46, -66, -21, 58 }; - private static final String STANDARD_SHA512_RESULT_STRING = "B42AF09057BAC1E2D41708E48A902E09B5FF7F12AB428A4FE86653C73DD248FB82F948A549F7B791A5B41915EE4D1EC3935357E4E2317250D0372AFA2EBEEB3A" - .toLowerCase(); - @Test public void testConstructor() { assertNotNull(new HmacUtils()); @@ -73,22 +48,22 @@ public class HmacUtilsTest { @Test public void testGetHMac() throws IOException { - Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, - HmacUtils.getHmacMd5(STANDARD_KEY_BYTES).doFinal(STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA1_RESULT_BYTES, - HmacUtils.getHmacSha1(STANDARD_KEY_BYTES).doFinal(STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA256_RESULT_BYTES, - HmacUtils.getHmacSha256(STANDARD_KEY_BYTES).doFinal(STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA384_RESULT_BYTES, - HmacUtils.getHmacSha384(STANDARD_KEY_BYTES).doFinal(STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA512_RESULT_BYTES, - HmacUtils.getHmacSha512(STANDARD_KEY_BYTES).doFinal(STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, + HmacUtils.getHmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES, + HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, + HmacUtils.getHmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES, + HmacUtils.getHmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES, + HmacUtils.getHmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); } @Test public void testHmacMd5Hex() throws IOException { - assertEquals("80070713463e7749b90c2dc24911e275", - HmacUtils.hmacMd5Hex(STANDARD_KEY_STRING, "The quick brown fox jumps over the lazy dog")); + assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING, + HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, "The quick brown fox jumps over the lazy dog")); assertEquals("750c783e6ab0b503eaa86e310a5db738", HmacUtils.hmacMd5Hex("Jefe", "what do ya want for nothing?")); assertEquals( "750c783e6ab0b503eaa86e310a5db738", @@ -98,8 +73,8 @@ public class HmacUtilsTest { @Test public void testHmacSha1Hex() throws IOException { - assertEquals(STANDARD_SHA1_RESULT_STRING, HmacUtils.hmacSha1Hex(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); - assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", HmacUtils.hmacSha1Hex(STANDARD_KEY_STRING, "")); + assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); + assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, "")); assertEquals("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", HmacUtils.hmacSha1Hex("Jefe", "what do ya want for nothing?")); assertEquals( @@ -110,44 +85,44 @@ public class HmacUtilsTest { @Test public void testHmacSha1UpdateWithByteArray() throws IOException { - final Mac mac = HmacUtils.getHmacSha1(STANDARD_KEY_BYTES); - HmacUtils.updateHmac(mac, STANDARD_PHRASE_BYTES); - assertEquals(STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); + final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES); + HmacUtils.updateHmac(mac, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); + assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); HmacUtils.updateHmac(mac, "".getBytes()); assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal())); } @Test public void testHmacSha1UpdateWithInpustream() throws IOException { - final Mac mac = HmacUtils.getHmacSha1(STANDARD_KEY_BYTES); - HmacUtils.updateHmac(mac, new ByteArrayInputStream(STANDARD_PHRASE_BYTES)); - assertEquals(STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); + final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES); + HmacUtils.updateHmac(mac, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); HmacUtils.updateHmac(mac, new ByteArrayInputStream("".getBytes())); assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal())); } @Test public void testHmacSha1UpdateWithString() throws IOException { - final Mac mac = HmacUtils.getHmacSha1(STANDARD_KEY_BYTES); - HmacUtils.updateHmac(mac, STANDARD_PHRASE_STRING); - assertEquals(STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); + final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES); + HmacUtils.updateHmac(mac, HmacAlgorithmsTest.STANDARD_PHRASE_STRING); + assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal())); HmacUtils.updateHmac(mac, ""); assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal())); } @Test public void testInitializedMac() throws IOException { - final Mac md5Mac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, STANDARD_KEY_BYTES); - final Mac md5Mac2 = HmacUtils.getInitializedMac("HmacMD5", STANDARD_KEY_BYTES); - Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac, STANDARD_PHRASE_STRING) + final Mac md5Mac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, HmacAlgorithmsTest.STANDARD_KEY_BYTES); + final Mac md5Mac2 = HmacUtils.getInitializedMac("HmacMD5", HmacAlgorithmsTest.STANDARD_KEY_BYTES); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac, HmacAlgorithmsTest.STANDARD_PHRASE_STRING) .doFinal()); - Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac2, STANDARD_PHRASE_STRING) + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac2, HmacAlgorithmsTest.STANDARD_PHRASE_STRING) .doFinal()); } @Test(expected = IllegalArgumentException.class) public void testInitializedMacNullAlgo() throws IOException { - HmacUtils.getInitializedMac((String) null, STANDARD_KEY_BYTES); + HmacUtils.getInitializedMac((String) null, HmacAlgorithmsTest.STANDARD_KEY_BYTES); } @Test(expected = IllegalArgumentException.class) @@ -162,22 +137,22 @@ public class HmacUtilsTest { @Test public void testMd5HMac() throws IOException { - Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, - HmacUtils.hmacMd5(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, - HmacUtils.hmacMd5(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertArrayEquals(STANDARD_MD5_RESULT_BYTES, - HmacUtils.hmacMd5(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); - Assert.assertEquals(STANDARD_MD5_RESULT_STRING, HmacUtils.hmacMd5Hex(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertEquals(STANDARD_MD5_RESULT_STRING, - HmacUtils.hmacMd5Hex(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertEquals(STANDARD_MD5_RESULT_STRING, - HmacUtils.hmacMd5Hex(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, + HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, + HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, + HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING, HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING, + HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING, + HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); } @Test(expected = IllegalArgumentException.class) public void testMd5HMacFail() throws IOException { - HmacUtils.hmacMd5((byte[]) null, STANDARD_PHRASE_BYTES); + HmacUtils.hmacMd5((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); } @Test(expected = IllegalArgumentException.class) @@ -192,85 +167,85 @@ public class HmacUtilsTest { @Test public void testSha1HMac() throws IOException { - Assert.assertArrayEquals(STANDARD_SHA1_RESULT_BYTES, - HmacUtils.hmacSha1(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA1_RESULT_BYTES, - HmacUtils.hmacSha1(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertArrayEquals(STANDARD_SHA1_RESULT_BYTES, - HmacUtils.hmacSha1(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); - Assert.assertEquals(STANDARD_SHA1_RESULT_STRING, - HmacUtils.hmacSha1Hex(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertEquals(STANDARD_SHA1_RESULT_STRING, - HmacUtils.hmacSha1Hex(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertEquals(STANDARD_SHA1_RESULT_STRING, - HmacUtils.hmacSha1Hex(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES, + HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES, + HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES, + HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, + HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, + HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, + HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); } @Test(expected = IllegalArgumentException.class) public void testSha1HMacFail() throws IOException { - HmacUtils.hmacSha1((byte[]) null, STANDARD_PHRASE_BYTES); + HmacUtils.hmacSha1((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); } @Test public void testSha256HMac() throws IOException { - Assert.assertArrayEquals(STANDARD_SHA256_RESULT_BYTES, - HmacUtils.hmacSha256(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA256_RESULT_BYTES, - HmacUtils.hmacSha256(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertArrayEquals(STANDARD_SHA256_RESULT_BYTES, - HmacUtils.hmacSha256(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); - Assert.assertEquals(STANDARD_SHA256_RESULT_STRING, - HmacUtils.hmacSha256Hex(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertEquals(STANDARD_SHA256_RESULT_STRING, - HmacUtils.hmacSha256Hex(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertEquals(STANDARD_SHA256_RESULT_STRING, - HmacUtils.hmacSha256Hex(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, + HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, + HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, + HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING, + HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING, + HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING, + HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); } @Test(expected = IllegalArgumentException.class) public void testSha256HMacFail() throws IOException { - HmacUtils.hmacSha256((byte[]) null, STANDARD_PHRASE_BYTES); + HmacUtils.hmacSha256((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); } @Test public void testSha384HMac() throws IOException { - Assert.assertArrayEquals(STANDARD_SHA384_RESULT_BYTES, - HmacUtils.hmacSha384(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA384_RESULT_BYTES, - HmacUtils.hmacSha384(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertArrayEquals(STANDARD_SHA384_RESULT_BYTES, - HmacUtils.hmacSha384(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); - Assert.assertEquals(STANDARD_SHA384_RESULT_STRING, - HmacUtils.hmacSha384Hex(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertEquals(STANDARD_SHA384_RESULT_STRING, - HmacUtils.hmacSha384Hex(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertEquals(STANDARD_SHA384_RESULT_STRING, - HmacUtils.hmacSha384Hex(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES, + HmacUtils.hmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES, + HmacUtils.hmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES, + HmacUtils.hmacSha384(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_STRING, + HmacUtils.hmacSha384Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_STRING, + HmacUtils.hmacSha384Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_STRING, + HmacUtils.hmacSha384Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); } @Test(expected = IllegalArgumentException.class) public void testSha384HMacFail() throws IOException { - HmacUtils.hmacSha384((byte[]) null, STANDARD_PHRASE_BYTES); + HmacUtils.hmacSha384((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); } @Test public void testSha512HMac() throws IOException { - Assert.assertArrayEquals(STANDARD_SHA512_RESULT_BYTES, - HmacUtils.hmacSha512(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertArrayEquals(STANDARD_SHA512_RESULT_BYTES, - HmacUtils.hmacSha512(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertArrayEquals(STANDARD_SHA512_RESULT_BYTES, - HmacUtils.hmacSha512(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); - Assert.assertEquals(STANDARD_SHA512_RESULT_STRING, - HmacUtils.hmacSha512Hex(STANDARD_KEY_BYTES, STANDARD_PHRASE_BYTES)); - Assert.assertEquals(STANDARD_SHA512_RESULT_STRING, - HmacUtils.hmacSha512Hex(STANDARD_KEY_BYTES, new ByteArrayInputStream(STANDARD_PHRASE_BYTES))); - Assert.assertEquals(STANDARD_SHA512_RESULT_STRING, - HmacUtils.hmacSha512Hex(STANDARD_KEY_STRING, STANDARD_PHRASE_STRING)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES, + HmacUtils.hmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES, + HmacUtils.hmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES, + HmacUtils.hmacSha512(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_STRING, + HmacUtils.hmacSha512Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_STRING, + HmacUtils.hmacSha512Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); + Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_STRING, + HmacUtils.hmacSha512Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); } @Test(expected = IllegalArgumentException.class) public void testSha512HMacFail() throws IOException { - HmacUtils.hmacSha512((byte[]) null, STANDARD_PHRASE_BYTES); + HmacUtils.hmacSha512((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); } } \ No newline at end of file