Skip to content

Instantly share code, notes, and snippets.

@GaspardP
Created June 4, 2015 15:21
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save GaspardP/fffdd54f563f67be8944 to your computer and use it in GitHub Desktop.
Save GaspardP/fffdd54f563f67be8944 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)
})
@Nusab19
Copy link

Nusab19 commented Dec 21, 2023

finally found what I was searching for.

Thanks a lot mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment