/** * 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 HmacAlgorithms algorithm, final byte[] key) { this(algorithm.getName(), 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.toString(), 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.toString(), key); }
/** * Returns whether this algorithm is available * *@param name the name to check * @return whether this algorithm is available * @since 1.11 */ public static boolean isAvailable(final HmacAlgorithms name) { try { Mac.getInstance(name.getName()); return true; } catch (final NoSuchAlgorithmException e) { return false; } }
/** * 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.toString(), key); }
/** * 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 HmacAlgorithms algorithm, final String key) { this(algorithm.getName(), StringUtils.getBytesUtf8(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 public void testAlgorithm() throws IOException, NoSuchAlgorithmException { final String algorithm = hmacAlgorithm.getName(); Assert.assertNotNull(algorithm); Assert.assertFalse(algorithm.isEmpty()); Assume.assumeTrue(HmacUtils.isAvailable(hmacAlgorithm)); Mac.getInstance(algorithm); }
@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 HmacAlgorithms algorithm, final byte[] key) { this(algorithm.getName(), key); }
/** * Returns whether this algorithm is available * *@param name the name to check * @return whether this algorithm is available * @since 1.11 */ public static boolean isAvailable(final HmacAlgorithms name) { try { Mac.getInstance(name.getName()); return true; } catch (final NoSuchAlgorithmException e) { return false; } }
/** * 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 HmacAlgorithms algorithm, final String key) { this(algorithm.getName(), StringUtils.getBytesUtf8(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); }