Last active
April 10, 2024 08:08
-
-
Save HaNdTriX/239f45939ee8b9f012861bb22808ba42 to your computer and use it in GitHub Desktop.
Convert a string to an sha256 hash in JavaScript
This file contains hidden or 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
/** | |
* Convert a string to an sha256 hash | |
* @param str {string} | |
* @returns {string} | |
*/ | |
async function sha256(str) { | |
const arrayBuffer = new TextEncoder("utf-8").encode(str) | |
const hashAsArrayBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer); | |
const uint8ViewOfHash = new Uint8Array(hashAsArrayBuffer); | |
return Array.from(uint8ViewOfHash).map((b) => b.toString(16).padStart(2, '0')).join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test