Created
August 19, 2019 20:29
-
-
Save eutobias/74e33ec105be841bd9e331b01403da1a to your computer and use it in GitHub Desktop.
SHA256
This file contains 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
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