@Test(expected = IllegalArgumentException.class) public void testSha256HMacFail() throws IOException { HmacUtils.hmacSha256((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES); }
@Test public void testSha256HMac() throws IOException { Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, HmacUtils.hmacSha256(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING, HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING, HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES))); Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_STRING, HmacUtils.hmacSha256Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest) { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static String hmacSha256Hex(final String key, final String valueToDigest) { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static String hmacSha256Hex(final String key, final String valueToDigest) { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static String hmacSha256Hex(final String key, final String valueToDigest) { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest) { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value as hex string (lowercase) * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest) { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest * <p> * The InputStream must not be null and will not be closed * </p> * @return HmacSHA256 MAC for the given key and value as 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. */ public static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest) throws IOException { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest * <p> * The InputStream must not be null and will not be closed * </p> * @return HmacSHA256 MAC for the given key and value as 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. */ public static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest) throws IOException { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest * <p> * The InputStream must not be null and will not be closed * </p> * @return HmacSHA256 MAC for the given key and value as 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. */ public static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest) throws IOException { return Hex.encodeHexString(hmacSha256(key, valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static byte[] hmacSha256(final String key, final String valueToDigest) { return hmacSha256(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static byte[] hmacSha256(final String key, final String valueToDigest) { return hmacSha256(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); }
/** * Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value. * * @param key * They key for the keyed digest (must not be null) * @param valueToDigest * The value (data) which should to digest (maybe empty or null) * @return HmacSHA256 MAC for the given key and value * @throws IllegalArgumentException * when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid. */ public static byte[] hmacSha256(final String key, final String valueToDigest) { return hmacSha256(StringUtils.getBytesUtf8(key), StringUtils.getBytesUtf8(valueToDigest)); }