Skip to content

Instantly share code, notes, and snippets.

@katopz
Last active September 16, 2019 10:22
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 katopz/18bfccd8d3513b4988306bc047f5e364 to your computer and use it in GitHub Desktop.
Save katopz/18bfccd8d3513b4988306bc047f5e364 to your computer and use it in GitHub Desktop.
sha-256-js
// Web : ref https://jameshfisher.com/2017/10/30/web-cryptography-api-hello-world/
async function sha256(str) {
const buf = await crypto.subtle.digest("SHA-256", new TextEncoder("utf-8").encode(str));
return Array.prototype.map.call(new Uint8Array(buf), x=>(('00'+x.toString(16)).slice(-2))).join('');
}
// Node
async function sha256(str) {
const crypto = require('crypto')
const hash = crypto.createHash('sha256')
hash.update(str)
const hex = hash.digest('hex')
return hex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment