/** * HMAC sha1 * * @param key the key must be at least 8 bytes in length. * @param in byte array to HMAC. * @return the hash * * @throws GeneralSecurityException */ public static byte[] hmacSha(byte[] key, byte[] in) throws GeneralSecurityException { return hmacSha(key, in, HMAC_TYPE); }
/** * HMAC sha1 * * @param key the key must be at least 8 bytes in length. * @param in byte array to HMAC. * @return the hash * * @throws GeneralSecurityException */ public static byte[] hmacSha(byte[] key, byte[] in) throws GeneralSecurityException { return hmacSha(key, in, HMAC_TYPE); }
/** * HMAC sha1 * * @param key the key must be at least 8 bytes in length. * @param in byte array to HMAC. * @return the hash * * @throws GeneralSecurityException */ public static byte[] hmacSha(byte[] key, byte[] in) throws GeneralSecurityException { return hmacSha(key, in, HMAC_TYPE); }
private static byte[] hash(final byte[] text, final byte[] key, final String algorithm) throws GeneralSecurityException { if (OAuth2Message.HMAC_SHA_1.equalsIgnoreCase(algorithm)) { return Crypto.hmacSha(key, text, HMACType.HMACSHA1.getName()); } return new byte[] {}; }
@Test public void testHmacSha384() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { 66, -117, 24, -112, 19, -58, 80, 27, -117, 23, 107, 41, -118, -3, 100, -61, 42, 77, 50, 70, -28, 85, -39, -55, 47, 42, 106, 116, -26, 72, 76, -101, 67, -37, -56, 5, -85, 117, -51, -95, -18, -100, 81, 69, 9, 105, 70, 99, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA384.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha256() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { 69, -128, -5, 20, 94, -46, -40, 46, 43, -24, -76, -93, -28, -70, 3, 93, 101, 124, 111, -56, 124, -38, 103, 41, 83, -53, -45, 36, -21, 73, -10, -32, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA256.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha512() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { -40, -114, 57, 41, -97, -13, 13, 106, -71, 72, -54, 97, -50, -109, -115, -24, -68, 82, 73, -97, 46, -21, -128, -40, 73, 41, 43, 61, 20, 35, 79, 90, -27, 83, -1, -64, -128, 49, -118, -117, 34, -63, -51, 87, -85, 120, -9, -107, 29, 106, -48, 51, 105, -56, 86, -52, 18, -45, -81, -6, 0, 16, 67, 90, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA512.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha1() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { -21, 2, 47, -101, 9, -40, 18, 43, 76, 117, -51, 115, -122, -91, 39, 26, -18, 122, 30, 90, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA1.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha384() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { 66, -117, 24, -112, 19, -58, 80, 27, -117, 23, 107, 41, -118, -3, 100, -61, 42, 77, 50, 70, -28, 85, -39, -55, 47, 42, 106, 116, -26, 72, 76, -101, 67, -37, -56, 5, -85, 117, -51, -95, -18, -100, 81, 69, 9, 105, 70, 99, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA384.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha256() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { 69, -128, -5, 20, 94, -46, -40, 46, 43, -24, -76, -93, -28, -70, 3, 93, 101, 124, 111, -56, 124, -38, 103, 41, 83, -53, -45, 36, -21, 73, -10, -32, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA256.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha1() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { -21, 2, 47, -101, 9, -40, 18, 43, 76, 117, -51, 115, -122, -91, 39, 26, -18, 122, 30, 90, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA1.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha384() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { 66, -117, 24, -112, 19, -58, 80, 27, -117, 23, 107, 41, -118, -3, 100, -61, 42, 77, 50, 70, -28, 85, -39, -55, 47, 42, 106, 116, -26, 72, 76, -101, 67, -37, -56, 5, -85, 117, -51, -95, -18, -100, 81, 69, 9, 105, 70, 99, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA384.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha512() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { -40, -114, 57, 41, -97, -13, 13, 106, -71, 72, -54, 97, -50, -109, -115, -24, -68, 82, 73, -97, 46, -21, -128, -40, 73, 41, 43, 61, 20, 35, 79, 90, -27, 83, -1, -64, -128, 49, -118, -117, 34, -63, -51, 87, -85, 120, -9, -107, 29, 106, -48, 51, 105, -56, 86, -52, 18, -45, -81, -6, 0, 16, 67, 90, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA512.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha512() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { -40, -114, 57, 41, -97, -13, 13, 106, -71, 72, -54, 97, -50, -109, -115, -24, -68, 82, 73, -97, 46, -21, -128, -40, 73, 41, 43, 61, 20, 35, 79, 90, -27, 83, -1, -64, -128, 49, -118, -117, 34, -63, -51, 87, -85, 120, -9, -107, 29, 106, -48, 51, 105, -56, 86, -52, 18, -45, -81, -6, 0, 16, 67, 90, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA512.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha1() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { -21, 2, 47, -101, 9, -40, 18, 43, 76, 117, -51, 115, -122, -91, 39, 26, -18, 122, 30, 90, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA1.getName()); assertArrayEquals(expected, hmac); }
@Test public void testHmacSha256() throws Exception { String key = "abcd1234"; String val = "your mother is a hedgehog"; byte[] expected = { 69, -128, -5, 20, 94, -46, -40, 46, 43, -24, -76, -93, -28, -70, 3, 93, 101, 124, 111, -56, 124, -38, 103, 41, 83, -53, -45, 36, -21, 73, -10, -32, }; byte[] hmac = Crypto.hmacSha(key.getBytes(), val.getBytes(),HMACType.HMACSHA256.getName()); assertArrayEquals(expected, hmac); }
public String wrap(Map<String, String> in) throws BlobCrypterException { try { byte[] encoded = serialize(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha(hmacKey, cipherText,hmacType.getName()); byte[] b64 = Base64.encodeBase64URLSafe(Bytes.concat(cipherText, hmac)); return CharsetUtil.newUtf8String(b64); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }
public String wrap(Map<String, String> in) throws BlobCrypterException { try { byte[] encoded = serialize(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha(hmacKey, cipherText,hmacType.getName()); byte[] b64 = Base64.encodeBase64URLSafe(Bytes.concat(cipherText, hmac)); return CharsetUtil.newUtf8String(b64); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }
public String wrap(Map<String, String> in) throws BlobCrypterException { try { byte[] encoded = serialize(in); byte[] cipherText = Crypto.aes128cbcEncrypt(cipherKey, encoded); byte[] hmac = Crypto.hmacSha(hmacKey, cipherText,hmacType.getName()); byte[] b64 = Base64.encodeBase64URLSafe(Bytes.concat(cipherText, hmac)); return CharsetUtil.newUtf8String(b64); } catch (GeneralSecurityException e) { throw new BlobCrypterException(e); } }