Skip to content

Instantly share code, notes, and snippets.

@eutobias
Created August 19, 2019 20:29
Show Gist options
  • Save eutobias/74e33ec105be841bd9e331b01403da1a to your computer and use it in GitHub Desktop.
Save eutobias/74e33ec105be841bd9e331b01403da1a to your computer and use it in GitHub Desktop.
SHA256
function sha256(message, cb) {
var encoder = new TextEncoder()
var data = encoder.encode(message)
crypto.subtle.digest('SHA-256', data).then(function(hash) {
var hashArray = Array.from(new Uint8Array(hash))
hashArray.forEach(function(b, i) {
hashArray[i] = hashArray[i].toString(16).padStart(2, '0')
})
cb(hashArray.join(''))
}).catch(function(err) {
console.log(new Error(err))
})
}
// USAGE
sha256('teste', function(hash) {
console.log(hash)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment