Return-Path:
- * 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 initializedMac
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 initializedMac
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