Skip to content

Instantly share code, notes, and snippets.

@amoossssss
Last active September 15, 2023 02:51
Show Gist options
  • Save amoossssss/0994a64869e8c861a86ed9c8f5b1e0ff to your computer and use it in GitHub Desktop.
Save amoossssss/0994a64869e8c861a86ed9c8f5b1e0ff to your computer and use it in GitHub Desktop.
Rhino HMAC (Pure JDK Standard Library)
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
function hmac(algorithm, key, message) {
var mac = javax.crypto.Mac.getInstance(algorithm);
mac.init(new javax.crypto.spec.SecretKeySpec(key, algorithm));
return mac.doFinal(message);
}
function generateHmac256(message, key) {
var bytes = hmac("HmacSHA256", key, message.getBytes());
return toHexString(bytes);
}
var valueToDigest = new java.lang.String("0123456789");
var key = (new java.lang.String("secret")).getBytes();
var messageDigest = generateHmac256(valueToDigest, key);
print(messageDigest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment