Skip to content

Instantly share code, notes, and snippets.

@glachancecmaisonneuve
Forked from lusbuab/sha-1.js
Last active April 11, 2023 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glachancecmaisonneuve/0773b153d470ec9daaba4e16cb88d760 to your computer and use it in GitHub Desktop.
Save glachancecmaisonneuve/0773b153d470ec9daaba4e16cb88d760 to your computer and use it in GitHub Desktop.
Calculating SHA-1 in the Browser
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