-
-
Save glachancecmaisonneuve/0773b153d470ec9daaba4e16cb88d760 to your computer and use it in GitHub Desktop.
Calculating SHA-1 in the Browser
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 sha1(str) { | |
const buffer = new TextEncoder("utf-8").encode(str); | |
const hash = await crypto.subtle.digest('SHA-1', buffer) | |
const hexCodes = []; | |
const view = new DataView(hash); | |
for (let i = 0; i < view.byteLength; i += 1) { | |
const byte = view.getUint8(i).toString(16).padStart(2, '0') | |
hexCodes.push(byte); | |
} | |
return hexCodes.join(''); | |
} | |
// await sha1('hello, world!') | |
// outputs: 1f09d30c707d53f3d16c530dd73d70a6ce7596a9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment