Skip to content

Instantly share code, notes, and snippets.

@LeoMcA
Last active August 29, 2015 14: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 LeoMcA/bc31922229cf39a7f9a6 to your computer and use it in GitHub Desktop.
Save LeoMcA/bc31922229cf39a7f9a6 to your computer and use it in GitHub Desktop.
Hash/checksum generation in client side JS
function createHexHash (algo, arrayBuffer, callback) {
if (Object.prototype.toString.call(arrayBuffer) == '[object String]') {
var encoder = new TextEncoder('utf-8')
arrayBuffer = encoder.encode(arrayBuffer)
}
crypto.subtle.digest(algo, arrayBuffer).then(function (hash) {
var hex = ""
var array = new Uint8Array(hash)
for (var i = 0; i < array.byteLength; i++) {
var byte = array[i]
var pad = ""
if (byte < 16) pad = "0"
hex += pad + byte.toString(16)
}
callback(hex)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment