Message Authentication Codes
Mac
Usage examples
KeyStore keystore = keystore("classpath:/keystore.p12", "password".toCharArray(), "PKCS12");
Key key = secretKey(keystore, "hmac", "password".toCharArray());
Mac mac = macBuilder().key(key).algorithm("HmacSHA256").build();
// raw bytes → Bytes
Bytes rawMac = mac.get(Bytes.from("Hello there".getBytes(UTF_8)));
// UTF-8 text → BASE64 string
String b64 = mac.get(Bytes.from("Hello there")).encode(BASE64);
// UTF-8 text → HEX string
String hex = mac.get(Bytes.from("Hello there")).encode(HEX);
// explicit charset → BASE64 string
String b64_2 = mac.get(Bytes.from("Hello 👋🏻", UTF_16)).encode(BASE64);
// Verify both parties produce the same MAC
Mac alice = macBuilder().key(key).algorithm("HmacSHA256").build();
Mac bob = macBuilder().key(key).algorithm("HmacSHA256").build();
Bytes aliceMac = alice.get(Bytes.from("Hi Bob"));
Bytes bobMac = bob.get(Bytes.from("Hi Bob"));
assertEquals(aliceMac, bobMac);
// compare as encoded strings
assertEquals(aliceMac.encode(BASE64), bobMac.encode(BASE64));Builder options
Interface
Input / output combinations
Input
How to construct
Consume output
Last updated
Was this helpful?