/** * Creates an instance using the provided algorithm type. * * @param algorithm to use * @param key the key to use * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. * @since 1.11 */ public HmacUtils(final String algorithm, final byte[] key) { this(getInitializedMac(algorithm, key)); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA512 algorithm. * <p> * Every implementation of the Java platform is <em>not</em> required to support this Mac algorithm. * </p> * * @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. * @deprecated (1.11) Use {@code getInitializedMac(HmacAlgorithms.HMAC_SHA_512, byte[])} */ @Deprecated public static Mac getHmacSha512(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_512, key); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA256 algorithm. * <p> * Every implementation of the Java platform is required to support this standard Mac algorithm. * </p> * * @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. * @deprecated (1.11) Use {@code getInitializedMac(HmacAlgorithms.HMAC_SHA_256, byte[])} */ @Deprecated public static Mac getHmacSha256(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_256, key); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA1 algorithm. * <p> * Every implementation of the Java platform is required to support this standard Mac algorithm. * </p> * * @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. * @deprecated (1.11) Use {@code getInitializedMac(HmacAlgorithms.HMAC_SHA_1, byte[])} */ @Deprecated public static Mac getHmacSha1(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_1, key); }
/** * Returns an initialized <code>Mac</code> for the HmacMD5 algorithm. * <p> * Every implementation of the Java platform is required to support this standard Mac algorithm. * </p> * * @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. * @deprecated (1.11) Use {@code getInitializedMac(HmacAlgorithms.HMAC_MD5, byte[])} */ @Deprecated public static Mac getHmacMd5(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_MD5, key); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA384 algorithm. * <p> * Every implementation of the Java platform is <em>not</em> required to support this Mac algorithm. * </p> * * @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. * @deprecated (1.11) Use {@code getInitializedMac(HmacAlgorithms.HMAC_SHA_384, byte[])} */ @Deprecated public static Mac getHmacSha384(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_384, key); }
/** * Returns an initialized <code>Mac</code> for the given <code>algorithm</code>. * * @param algorithm * the name of the algorithm requested. See * <a href= "http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA" * >Appendix A in the Java Cryptography Architecture Reference Guide</a> 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. * @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 static Mac getInitializedMac(final HmacAlgorithms algorithm, final byte[] key) { return getInitializedMac(algorithm.getName(), key); }
@Test(expected = IllegalArgumentException.class) public void testInitializedMacNullKey() throws IOException { HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, null); }
@Test(expected = IllegalArgumentException.class) public void testGetHmacEmptyKey() { HmacUtils.getInitializedMac(hmacAlgorithm, EMPTY_BYTE_ARRAY); }
@Test(expected = IllegalArgumentException.class) public void testInitializedMacNullAlgo() throws IOException { HmacUtils.getInitializedMac((String) null, HmacAlgorithmsTest.STANDARD_KEY_BYTES); }
@Test(expected = IllegalArgumentException.class) public void testGetHmacNullKey() { HmacUtils.getInitializedMac(hmacAlgorithm, null); }
@Test public void testInitializedMac() throws IOException { 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(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac2, HmacAlgorithmsTest.STANDARD_PHRASE_STRING) .doFinal()); }
@Test(expected = IllegalArgumentException.class) public void testInternalNoSuchAlgorithmException() { HmacUtils.getInitializedMac("Bogus Bogus", StringUtils.getBytesUtf8("akey")); }
@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()); }
/** * Creates an instance using the provided algorithm type. * * @param algorithm to use * @param key the key to use * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. * @since 1.11 */ public HmacUtils(final String algorithm, final byte[] key) { this(getInitializedMac(algorithm, key)); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA256 algorithm. * <p> * Every implementation of the Java platform is required to support this standard Mac algorithm. * </p> * * @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 static Mac getHmacSha256(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_256, key); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA384 algorithm. * <p> * Every implementation of the Java platform is <em>not</em> required to support this Mac algorithm. * </p> * * @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 static Mac getHmacSha384(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_384, key); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA512 algorithm. * <p> * Every implementation of the Java platform is <em>not</em> required to support this Mac algorithm. * </p> * * @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 static Mac getHmacSha512(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_512, key); }
/** * Returns an initialized <code>Mac</code> for the HmacSHA1 algorithm. * <p> * Every implementation of the Java platform is required to support this standard Mac algorithm. * </p> * * @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 static Mac getHmacSha1(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_SHA_1, key); }
/** * Returns an initialized <code>Mac</code> for the HmacMD5 algorithm. * <p> * Every implementation of the Java platform is required to support this standard Mac algorithm. * </p> * * @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 static Mac getHmacMd5(final byte[] key) { return getInitializedMac(HmacAlgorithms.HMAC_MD5, key); }