Created
March 23, 2019 07:52
-
-
Save Noobgam/c077ac1dfd26e9ca91f97cd9d1088314 to your computer and use it in GitHub Desktop.
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 sha256(message) { | |
// encode as UTF-8 | |
const msgBuffer = new TextEncoder('utf-8').encode(message); | |
// hash the message | |
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer); | |
// convert ArrayBuffer to Array | |
const hashArray = Array.from(new Uint8Array(hashBuffer)); | |
// convert bytes to hex string | |
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join(''); | |
return hashHex; | |
} | |
sha256('abc').then(hash => console.log(hash)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment