Created
October 15, 2018 01:23
-
-
Save themikefuller/c2f597d98aff6813714eb4ac8c65b2c3 to your computer and use it in GitHub Desktop.
SHA-1 and SHA-256 Hash Function in JavaScript using crypto.subtle.digest
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(text) { | |
let data = new TextEncoder().encode(text.toString()); | |
let digest = await crypto.subtle.digest("SHA-1",data); | |
return Array.from(new Uint8Array(digest)).map(val=>{return ('00' + val.toString(16)).slice(-2)}).join(''); | |
} | |
async function SHA256(text) { | |
let data = new TextEncoder().encode(text.toString()); | |
let digest = await crypto.subtle.digest("SHA-256",data); | |
return Array.from(new Uint8Array(digest)).map(val=>{return ('00' + val.toString(16)).slice(-2)}).join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this solution! Really helped me understand the crypto.subtle.digest workflow.
I ended up iterating on your approach with some ES6+ optimizations - thought I'd share back in case it's useful for others: