@Test public void testGetHMac() throws IOException { Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.getHmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES, HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES, HmacUtils.getHmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES, HmacUtils.getHmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES, HmacUtils.getHmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)); }
/** * Returns a HmacSHA512 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 HmacSHA512 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[] hmacSha512(final byte[] key, final byte[] valueToDigest) { try { return getHmacSha512(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { // cannot happen throw new IllegalArgumentException(e); } }
/** * Returns a HmacSHA512 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 HmacSHA512 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[] hmacSha512(final byte[] key, final byte[] valueToDigest) { try { return getHmacSha512(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { // cannot happen throw new IllegalArgumentException(e); } }
/** * Returns a HmacSHA512 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 HmacSHA512 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[] hmacSha512(final byte[] key, final byte[] valueToDigest) { try { return getHmacSha512(key).doFinal(valueToDigest); } catch (final IllegalStateException e) { // cannot happen throw new IllegalArgumentException(e); } }
@NotNull public static String getToken(@NotNull final String identifier, @NotNull final String primaryOrSecondaryKey, @NotNull final Date expiryDate) { final Mac sha512Hmac = HmacUtils.getHmacSha512(primaryOrSecondaryKey.getBytes(UTF_8)); final String dataToSign = String.format("%s\n%s", identifier, DateUtilities.formatDate(expiryDate)); final byte[] encodedBytes = Base64.encodeBase64(sha512Hmac.doFinal(dataToSign.getBytes(UTF_8))); final String encodedString = new String(encodedBytes, UTF_8); return String.format(SHARED_ACCESS_SIGNATURE, identifier, DateUtilities.formatDate(expiryDate), encodedString); }
/** * Returns a HmacSHA512 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 * <p> * The InputStream must not be null and will not be closed * </p> * @return HmacSHA512 MAC for the given key and value * @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 byte[] hmacSha512(final byte[] key, final InputStream valueToDigest) throws IOException { return updateHmac(getHmacSha512(key), valueToDigest).doFinal(); }
/** * Returns a HmacSHA512 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 * <p> * The InputStream must not be null and will not be closed * </p> * @return HmacSHA512 MAC for the given key and value * @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 byte[] hmacSha512(final byte[] key, final InputStream valueToDigest) throws IOException { return updateHmac(getHmacSha512(key), valueToDigest).doFinal(); }
/** * Returns a HmacSHA512 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 * <p> * The InputStream must not be null and will not be closed * </p> * @return HmacSHA512 MAC for the given key and value * @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 byte[] hmacSha512(final byte[] key, final InputStream valueToDigest) throws IOException { return updateHmac(getHmacSha512(key), valueToDigest).doFinal(); }