Created
February 27, 2021 07:03
-
-
Save mstorus/ff42807e56e965622efde47d5e321629 to your computer and use it in GitHub Desktop.
use browser subtle crypto to sign some text with hmac-sha1
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
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