Created
June 4, 2015 15:21
-
-
Save GaspardP/fffdd54f563f67be8944 to your computer and use it in GitHub Desktop.
SHA-256 with Javascript and Web Crypto
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
// Computes the SHA-256 digest of a string with Web Crypto | |
// Source: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest | |
function sha256(str) { | |
// Get the string as arraybuffer. | |
var buffer = new TextEncoder("utf-8").encode(str) | |
return crypto.subtle.digest("SHA-256", buffer).then(function(hash) { | |
return hex(hash) | |
}) | |
} | |
function hex(buffer) { | |
var digest = '' | |
var view = new DataView(buffer) | |
for(var i = 0; i < view.byteLength; i += 4) { | |
// We use getUint32 to reduce the number of iterations (notice the `i += 4`) | |
var value = view.getUint32(i) | |
// toString(16) will transform the integer into the corresponding hex string | |
// but will remove any initial "0" | |
var stringValue = value.toString(16) | |
// One Uint32 element is 4 bytes or 8 hex chars (it would also work with 4 | |
// chars for Uint16 and 2 chars for Uint8) | |
var padding = '00000000' | |
var paddedValue = (padding + stringValue).slice(-padding.length) | |
digest += paddedValue | |
} | |
return digest | |
} | |
// Should output "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2" | |
// We can check the result with: | |
// python -c 'from hashlib import sha256;print sha256("foobar").hexdigest()' | |
sha256("foobar").then(function(digest) { | |
console.log(digest) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
finally found what I was searching for.
Thanks a lot mate!