Author: sebb Date: Mon May 23 12:30:06 2016 New Revision: 1745174 URL: http://svn.apache.org/viewvc?rev=1745174&view=rev Log: CODEC-218 Refactor HmacUtils methods into the HmacAlgorithms enum Reverted, because not all algorithms can be supported by the enum
Modified: commons/proper/codec/trunk/src/changes/changes.xml commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/HmacAlgorithms.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=1745174&r1=1745173&r2=1745174&view=diff ============================================================================== --- commons/proper/codec/trunk/src/changes/changes.xml (original) +++ commons/proper/codec/trunk/src/changes/changes.xml Mon May 23 12:30:06 2016 @@ -53,7 +53,6 @@ The <action> type attribute can be add,u <action issue="CODEC-199" dev="ggregory" type="fix" due-to="Yossi Tamari">Bug in HW rule in Soundex</action> <action issue="CODEC-209" dev="ggregory" type="fix" due-to="Gary Gregory">Javadoc for SHA-224 DigestUtils methods should mention Java 1.8.0 restriction instead of 1.4.0.</action> <action issue="CODEC-219" dev="ggregory" type="fix" due-to="Gary Gregory, Sebb">Don't deprecate Charsets Charset constants in favor of Java 7's java.nio.charset.StandardCharsets</action> - <action issue="CODEC-218" dev="ggregory" type="add" due-to="Gary Gregory">Refactor HmacUtils methods into the HmacAlgorithms enum</action> <action issue="CODEC-217" dev="ggregory" type="add" due-to="Gary Gregory">Add HmacAlgorithms.HMAC_SHA_224 (Java 8 only)</action> <action issue="CODEC-213" dev="ggregory" type="add" due-to="Gary Gregory">Support JEP 287: SHA-3 Hash Algorithms</action> <action issue="CODEC-212" dev="ggregory" type="add" due-to="Gary Gregory">Create a minimal Digest command line utility: org.apache.commons.codec.digest.Digest</action> 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=1745174&r1=1745173&r2=1745174&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 23 12:30:06 2016 @@ -17,16 +17,6 @@ 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 <cite>Java Cryptography Architecture Standard Algorithm Name * Documentation</cite>. @@ -102,25 +92,6 @@ public enum HmacAlgorithms { } /** - * Returns an initialized <code>Mac</code> for the this algorithm. - * <p> - * Every implementation of the Java platform is required to support this standard Mac algorithm. - * </p> - * - * @param key - * The 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. - * @since 1.11 - */ - public Mac getHmac(final byte[] key) { - return HmacUtils.getInitializedMac(name, key); - } - - /** * Gets the algorithm name. * * @return the algorithm name. @@ -131,127 +102,6 @@ public enum HmacAlgorithms { } /** - * Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value. - * - * @param key - * The key for the keyed digest (must not be null) - * @param valueToDigest - * The value (data) 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. - * @since 1.11 - */ - 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 - * The key for the keyed digest (must not be null) - * @param valueToDigest - * The value (data) 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. - * @since 1.11 - */ - 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. - * The Strings are converted to bytes using the UTF-8 charset. - * - * @param key - * The key for the keyed digest (must not be null) - * @param valueToDigest - * The value (data) 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. - * @since 1.11 - */ - 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 - * The key for the keyed digest (must not be null) - * @param valueToDigest - * The value (data) 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. - * @since 1.11 - */ - 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 - * The key for the keyed digest (must not be null) - * @param valueToDigest - * The value (data) 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. - * @since 1.11 - */ - 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 - * The key for the keyed digest (must not be null) - * @param valueToDigest - * The value (data) 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. - * @since 1.11 - */ - 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 - * @since 1.11 - */ - public boolean isAvailable() { - try { - Mac.getInstance(name); - return true; - } catch (NoSuchAlgorithmException e) { - return false; - } - } - - /** * The algorithm name * * @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/SunProviders.html#SunJCEProvider">