Last active
September 15, 2023 02:51
-
-
Save amoossssss/0994a64869e8c861a86ed9c8f5b1e0ff to your computer and use it in GitHub Desktop.
Rhino HMAC (Pure JDK Standard Library)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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