Skip to content

Instantly share code, notes, and snippets.

@tobz-nz
Created June 3, 2026 05:06
Show Gist options
  • Select an option

  • Save tobz-nz/762e63c8616a8c2e0f74cf2a1f3597ff to your computer and use it in GitHub Desktop.

Select an option

Save tobz-nz/762e63c8616a8c2e0f74cf2a1f3597ff to your computer and use it in GitHub Desktop.
Quick javscript hash function - supports SHA algorythms
/**
* 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