Skip to content

Instantly share code, notes, and snippets.

@gtechsltn
Forked from GaspardP/sha256.js
Created March 12, 2024 07: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 gtechsltn/610d7750237719ef78b0d314f830d0dc to your computer and use it in GitHub Desktop.
Save gtechsltn/610d7750237719ef78b0d314f830d0dc to your computer and use it in GitHub Desktop.
SHA-256 with Javascript and Web Crypto
// 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