Skip to content

Instantly share code, notes, and snippets.

@Noobgam
Created March 23, 2019 07:52
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 Noobgam/c077ac1dfd26e9ca91f97cd9d1088314 to your computer and use it in GitHub Desktop.
Save Noobgam/c077ac1dfd26e9ca91f97cd9d1088314 to your computer and use it in GitHub Desktop.
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