Skip to content

Instantly share code, notes, and snippets.

@ewalk153
Last active July 6, 2024 02:13
Show Gist options
  • Save ewalk153/9127b47f02a6baf0aecb2012428daa76 to your computer and use it in GitHub Desktop.
Save ewalk153/9127b47f02a6baf0aecb2012428daa76 to your computer and use it in GitHub Desktop.
(async (secret, body) => {
let enc = new TextEncoder("utf-8");
let algorithm = { name: "HMAC", hash: "SHA-256" };
let key = await crypto.subtle.importKey("raw", enc.encode(secret), algorithm, false, ["sign", "verify"]);
let signature = await crypto.subtle.sign(algorithm.name, key, enc.encode(body));
let hexString = Array.from(new Uint8Array(signature)).map(byte => byte.toString(16).padStart(2, '0')).join('');
console.log(hexString);
})("my secret and secure key", "input message");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment