Skip to content

Instantly share code, notes, and snippets.

@mstorus
Created February 27, 2021 07:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstorus/ff42807e56e965622efde47d5e321629 to your computer and use it in GitHub Desktop.
Save mstorus/ff42807e56e965622efde47d5e321629 to your computer and use it in GitHub Desktop.
use browser subtle crypto to sign some text with hmac-sha1
async function cryptoSign(message) {
const key = await window.crypto.subtle.generateKey(
{
name: "HMAC",
hash: { name: "SHA-512" },
},
true,
["sign", "verify"]
);
const encodedText = new TextEncoder().encode(message);
const signature = await window.crypto.subtle.sign("HMAC", key, encodedText);
const signatureText = btoa(String.fromCharCode(...new Uint8Array(signature)));
return signatureText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment