Skip to content

Instantly share code, notes, and snippets.

@lusbuab
Last active April 11, 2023 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lusbuab/261a161dc070d214499b9f6ba737363f to your computer and use it in GitHub Desktop.
Save lusbuab/261a161dc070d214499b9f6ba737363f to your computer and use it in GitHub Desktop.
Calculating SHA-1 in the Browser
async function sha1(str) {
const enc = new TextEncoder();
const hash = await crypto.subtle.digest('SHA-1', enc.encode(str));
return Array.from(new Uint8Array(hash))
.map(v => v.toString(16).padStart(2, '0'))
.join('');
}
// await sha1('hello, world!');
// outputs: 1f9d3c707d53f3d16c53dd73d70a6ce7596a9
@glachancecmaisonneuve
Copy link

pad is not a function. did you mean padStart?

@lusbuab
Copy link
Author

lusbuab commented Apr 11, 2023

@glachancecmaisonneuve yes, thanks for pointing this out. I've updated the code accordingly. Hopefully noone got confused by this in the 5 years since I've posted this 🫣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment