1717
1818package 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+
2030/**
2131 * Standard {@link HmacUtils} algorithm names from the <cite>Java Cryptography Architecture Standard Algorithm Name
2232 * Documentation</cite>.
2535 * <strong>Note: Not all JCE implementations support all the algorithms in this enum.</strong>
2636 * </p>
2737 *
28- * @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html">Java Cryptography
29- * Architecture Standard Algorithm Name Documentation</a>
38+ * @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/SunProviders.html#SunJCEProvider"> Java
39+ * 6 Cryptography Architecture Sun Providers Documentation</a>
40+ * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/SunProviders.html#SunJCEProvider"> Java
41+ * 7 Cryptography Architecture Sun Providers Documentation</a>
42+ * @see <a href="http://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html#SunJCEProvider"> Java
43+ * 8 Cryptography Architecture Sun Providers Documentation</a>
3044 * @since 1.10
3145 * @version $Id$
3246 */
@@ -51,7 +65,7 @@ public enum HmacAlgorithms {
5165 /**
5266 * The HmacSHA256 Message Authentication Code (MAC) algorithm specified in RFC 2104 and FIPS PUB 180-2.
5367 * <p>
54- * Every implementation of the Java 8 platform is required to support this standard MAC algorithm.
68+ * Every implementation of the Java platform is required to support this standard MAC algorithm.
5569 * </p>
5670 */
5771 HMAC_SHA_224 ("HmacSHA224" ),
@@ -80,23 +94,181 @@ public enum HmacAlgorithms {
8094 */
8195 HMAC_SHA_512 ("HmacSHA512" );
8296
83- private final String algorithm ;
97+ private final String name ;
8498
8599 private HmacAlgorithms (final String algorithm ) {
86- this .algorithm = algorithm ;
100+ this .name = algorithm ;
101+ }
102+
103+ /**
104+ * Returns an initialized <code>Mac</code> for the this algorithm.
105+ * <p>
106+ * Every implementation of the Java platform is required to support this standard Mac algorithm.
107+ * </p>
108+ *
109+ * @param key
110+ * They key for the keyed digest (must not be null)
111+ * @return A Mac instance initialized with the given key.
112+ * @see Mac#getInstance(String)
113+ * @see Mac#init(Key)
114+ * @throws IllegalArgumentException
115+ * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
116+ */
117+ public Mac getHmac (final byte [] key ) {
118+ return HmacUtils .getInitializedMac (name , key );
119+ }
120+
121+ /**
122+ * Returns an initialized <code>Mac</code> for this algorithm.
123+ *
124+ * @param key
125+ * They key for the keyed digest (must not be null)
126+ * @return A Mac instance initialized with the given key.
127+ * @see Mac#getInstance(String)
128+ * @see Mac#init(Key)
129+ * @throws IllegalArgumentException
130+ * when key is null or invalid.
131+ */
132+ public Mac getInitializedMac (final byte [] key ) {
133+ return HmacUtils .getInitializedMac (name , key );
134+ }
135+
136+ /**
137+ * Gets the algorithm name.
138+ *
139+ * @return the algorithm name.
140+ */
141+ public String getName () {
142+ return name ;
143+ }
144+
145+ /**
146+ * Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value.
147+ *
148+ * @param key
149+ * They key for the keyed digest (must not be null)
150+ * @param valueToDigest
151+ * The value (data) which should to digest (maybe empty or null)
152+ * @return HMAC for the given key and value
153+ * @throws IllegalArgumentException
154+ * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
155+ */
156+ public byte [] hmac (final byte [] key , final byte [] valueToDigest ) {
157+ try {
158+ return getHmac (key ).doFinal (valueToDigest );
159+ } catch (final IllegalStateException e ) {
160+ // cannot happen
161+ throw new IllegalArgumentException (e );
162+ }
163+ }
164+
165+ /**
166+ * Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value.
167+ *
168+ * @param key
169+ * They key for the keyed digest (must not be null)
170+ * @param valueToDigest
171+ * The value (data) which should to digest. The InputStream must not be null and will not be closed.
172+ * @return HMAC for the given key and value
173+ * @throws IOException
174+ * If an I/O error occurs.
175+ * @throws IllegalArgumentException
176+ * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
177+ */
178+ public byte [] hmac (final byte [] key , final InputStream valueToDigest ) throws IOException {
179+ return HmacUtils .updateHmac (getHmac (key ), valueToDigest ).doFinal ();
180+ }
181+
182+ /**
183+ * Returns a keyed-Hash Message Authentication Code (HMAC) for the given key and value.
184+ *
185+ * @param key
186+ * They key for the keyed digest (must not be null)
187+ * @param valueToDigest
188+ * The value (data) which should to digest (maybe empty or null)
189+ * @return HMAC for the given key and value
190+ * @throws IllegalArgumentException
191+ * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
192+ */
193+ public byte [] hmac (final String key , final String valueToDigest ) {
194+ return hmac (StringUtils .getBytesUtf8 (key ), StringUtils .getBytesUtf8 (valueToDigest ));
195+ }
196+
197+ /**
198+ * Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value.
199+ *
200+ * @param key
201+ * They key for the keyed digest (must not be null)
202+ * @param valueToDigest
203+ * The value (data) which should to digest (maybe empty or null)
204+ * @return HMAC for the given key and value as a hex string (lowercase)
205+ * @throws IllegalArgumentException
206+ * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
207+ */
208+ public String hmacHex (final byte [] key , final byte [] valueToDigest ) {
209+ return Hex .encodeHexString (hmac (key , valueToDigest ));
210+ }
211+
212+ /**
213+ * Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value.
214+ *
215+ * @param key
216+ * They key for the keyed digest (must not be null)
217+ * @param valueToDigest
218+ * The value (data) which should to digest. The InputStream must not be null and will not be closed.
219+ * @return HMAC for the given key and value as a hex string (lowercase)
220+ * @throws IOException
221+ * If an I/O error occurs.
222+ * @throws IllegalArgumentException
223+ * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
224+ */
225+ public String hmacHex (final byte [] key , final InputStream valueToDigest ) throws IOException {
226+ return Hex .encodeHexString (hmac (key , valueToDigest ));
227+ }
228+
229+ /**
230+ * Returns a keyed-Hash Message Authentication Code (HMAC) as a hex string (lowercase) for the given key and value.
231+ *
232+ * @param key
233+ * They key for the keyed digest (must not be null)
234+ * @param valueToDigest
235+ * The value (data) which should to digest (maybe empty or null)
236+ * @return HMAC for the given key and value as a hex string (lowercase)
237+ * @throws IllegalArgumentException
238+ * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
239+ */
240+ public String hmacHex (final String key , final String valueToDigest ) {
241+ return Hex .encodeHexString (hmac (key , valueToDigest ));
242+ }
243+
244+ /**
245+ * Returns whether this algorithm is available
246+ *
247+ * @return whether this algorithm is available
248+ */
249+ public boolean isAvailable () {
250+ try {
251+ Mac .getInstance (name );
252+ return true ;
253+ } catch (NoSuchAlgorithmException e ) {
254+ return false ;
255+ }
87256 }
88257
89258 /**
90259 * The algorithm name
91260 *
92- * @see <a
93- * href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/SunProviders.html#SunJCEProvider">Java
94- * Cryptography Architecture Sun Providers Documentation</a>
261+ * @see <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/SunProviders.html#SunJCEProvider">
262+ * Java 6 Cryptography Architecture Sun Providers Documentation</a>
263+ * @see <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/SunProviders.html#SunJCEProvider">
264+ * Java 7 Cryptography Architecture Sun Providers Documentation</a>
265+ * @see <a href="http://docs.oracle.com/javase/8/docs/technotes/guides/security/SunProviders.html#SunJCEProvider">
266+ * Java 8 Cryptography Architecture Sun Providers Documentation</a>
95267 * @return The algorithm name ("HmacSHA512" for example)
96268 */
97269 @ Override
98270 public String toString () {
99- return algorithm ;
271+ return name ;
100272 }
101-
273+
102274}
0 commit comments