Skip to content

Commit c7b540b

Browse files
committed
CODEC-218 Refactor HmacUtils methods into the HmacAlgorithms enum
Reverted, because not all algorithms can be supported by the enum git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1745174 13f79535-47bb-0310-9956-ffa450edef68
1 parent 410ab04 commit c7b540b

2 files changed

Lines changed: 0 additions & 151 deletions

File tree

src/changes/changes.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ The <action> type attribute can be add,update,fix,remove.
5353
<action issue="CODEC-199" dev="ggregory" type="fix" due-to="Yossi Tamari">Bug in HW rule in Soundex</action>
5454
<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>
5555
<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>
56-
<action issue="CODEC-218" dev="ggregory" type="add" due-to="Gary Gregory">Refactor HmacUtils methods into the HmacAlgorithms enum</action>
5756
<action issue="CODEC-217" dev="ggregory" type="add" due-to="Gary Gregory">Add HmacAlgorithms.HMAC_SHA_224 (Java 8 only)</action>
5857
<action issue="CODEC-213" dev="ggregory" type="add" due-to="Gary Gregory">Support JEP 287: SHA-3 Hash Algorithms</action>
5958
<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>

src/main/java/org/apache/commons/codec/digest/HmacAlgorithms.java

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717

1818
package org.apache.commons.codec.digest;
1919

20-
import java.io.IOException;
21-
import java.io.InputStream;
22-
import java.security.Key;
23-
import java.security.NoSuchAlgorithmException;
24-
25-
import javax.crypto.Mac;
26-
27-
import org.apache.commons.codec.binary.Hex;
28-
import org.apache.commons.codec.binary.StringUtils;
29-
3020
/**
3121
* Standard {@link HmacUtils} algorithm names from the <cite>Java Cryptography Architecture Standard Algorithm Name
3222
* Documentation</cite>.
@@ -101,25 +91,6 @@ private HmacAlgorithms(final String algorithm) {
10191
this.name = algorithm;
10292
}
10393

104-
/**
105-
* Returns an initialized <code>Mac</code> for the this algorithm.
106-
* <p>
107-
* Every implementation of the Java platform is required to support this standard Mac algorithm.
108-
* </p>
109-
*
110-
* @param key
111-
* The key for the keyed digest (must not be null)
112-
* @return A Mac instance initialized with the given key.
113-
* @see Mac#getInstance(String)
114-
* @see Mac#init(Key)
115-
* @throws IllegalArgumentException
116-
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
117-
* @since 1.11
118-
*/
119-
public Mac getHmac(final byte[] key) {
120-
return HmacUtils.getInitializedMac(name, key);
121-
}
122-
12394
/**
12495
* Gets the algorithm name.
12596
*
@@ -130,127 +101,6 @@ public String getName() {
130101
return name;
131102
}
132103

133-
/**
134-
* Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value.
135-
*
136-
* @param key
137-
* The key for the keyed digest (must not be null)
138-
* @param valueToDigest
139-
* The value (data) to digest (maybe empty or null)
140-
* @return HMAC for the given key and value
141-
* @throws IllegalArgumentException
142-
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
143-
* @since 1.11
144-
*/
145-
public byte[] hmac(final byte[] key, final byte[] valueToDigest) {
146-
try {
147-
return getHmac(key).doFinal(valueToDigest);
148-
} catch (final IllegalStateException e) {
149-
// cannot happen
150-
throw new IllegalArgumentException(e);
151-
}
152-
}
153-
154-
/**
155-
* Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value.
156-
*
157-
* @param key
158-
* The key for the keyed digest (must not be null)
159-
* @param valueToDigest
160-
* The value (data) to digest. The InputStream must not be null and will not be closed.
161-
* @return HMAC for the given key and value
162-
* @throws IOException
163-
* If an I/O error occurs.
164-
* @throws IllegalArgumentException
165-
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
166-
* @since 1.11
167-
*/
168-
public byte[] hmac(final byte[] key, final InputStream valueToDigest) throws IOException {
169-
return HmacUtils.updateHmac(getHmac(key), valueToDigest).doFinal();
170-
}
171-
172-
/**
173-
* Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value.
174-
* The Strings are converted to bytes using the UTF-8 charset.
175-
*
176-
* @param key
177-
* The key for the keyed digest (must not be null)
178-
* @param valueToDigest
179-
* The value (data) to digest (maybe empty or null)
180-
* @return HMAC for the given key and value
181-
* @throws IllegalArgumentException
182-
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
183-
* @since 1.11
184-
*/
185-
public byte[] hmac(final String key, final String valueToDigest) {
186-
return hmac(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest));
187-
}
188-
189-
/**
190-
* Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value.
191-
*
192-
* @param key
193-
* The key for the keyed digest (must not be null)
194-
* @param valueToDigest
195-
* The value (data) to digest (maybe empty or null)
196-
* @return HMAC for the given key and value as a hex string (lowercase)
197-
* @throws IllegalArgumentException
198-
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
199-
* @since 1.11
200-
*/
201-
public String hmacHex(final byte[] key, final byte[] valueToDigest) {
202-
return Hex.encodeHexString(hmac(key, valueToDigest));
203-
}
204-
205-
/**
206-
* Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value.
207-
*
208-
* @param key
209-
* The key for the keyed digest (must not be null)
210-
* @param valueToDigest
211-
* The value (data) to digest. The InputStream must not be null and will not be closed.
212-
* @return HMAC for the given key and value as a hex string (lowercase)
213-
* @throws IOException
214-
* If an I/O error occurs.
215-
* @throws IllegalArgumentException
216-
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
217-
* @since 1.11
218-
*/
219-
public String hmacHex(final byte[] key, final InputStream valueToDigest) throws IOException {
220-
return Hex.encodeHexString(hmac(key, valueToDigest));
221-
}
222-
223-
/**
224-
* Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value.
225-
*
226-
* @param key
227-
* The key for the keyed digest (must not be null)
228-
* @param valueToDigest
229-
* The value (data) to digest (maybe empty or null)
230-
* @return HMAC for the given key and value as a hex string (lowercase)
231-
* @throws IllegalArgumentException
232-
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
233-
* @since 1.11
234-
*/
235-
public String hmacHex(final String key, final String valueToDigest) {
236-
return Hex.encodeHexString(hmac(key, valueToDigest));
237-
}
238-
239-
/**
240-
* Returns whether this algorithm is available
241-
*
242-
* @return whether this algorithm is available
243-
* @since 1.11
244-
*/
245-
public boolean isAvailable() {
246-
try {
247-
Mac.getInstance(name);
248-
return true;
249-
} catch (NoSuchAlgorithmException e) {
250-
return false;
251-
}
252-
}
253-
254104
/**
255105
* The algorithm name
256106
*

0 commit comments

Comments
 (0)