Created
June 3, 2026 05:06
-
-
Save tobz-nz/762e63c8616a8c2e0f74cf2a1f3597ff to your computer and use it in GitHub Desktop.
Quick javscript hash function - supports SHA algorythms
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
| /** | |
| * Usage: hash('string to hash', 'sha-512') | |
| */ | |
| async function hash(str, algorithm = 'SHA-256') { | |
| const encoder = new TextEncoder(); // defaults to UTF-8 | |
| const uint8 = encoder.encode(str); // Uint8Array view | |
| const arrayBuffer = uint8.buffer; // ArrayBuffer | |
| const hashAsArrayBuffer = await crypto.subtle.digest(algorithm, arrayBuffer) | |
| const uint8ViewOfHash = new Uint8Array(hashAsArrayBuffer) | |
| return 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