Skip to content

Instantly share code, notes, and snippets.

@jaimegmx8
Created July 10, 2019 22:15
Show Gist options
  • Save jaimegmx8/c5e7512f1b94ff41abda377fdd64c937 to your computer and use it in GitHub Desktop.
Save jaimegmx8/c5e7512f1b94ff41abda377fdd64c937 to your computer and use it in GitHub Desktop.
Javascript native sha1 hex
function hex(buffer) {
var hexCodes = []
var view = new DataView(buffer)
for (var i = 0; i < view.byteLength; i += 4) {
var value = view.getUint32(i).toString(16)
var padding = '00000000'
var code = (padding + value).slice(-padding.length)
hexCodes.push(code)
}
return hexCodes.join('')
}
async function sha(str) {
var buffer = new TextEncoder('utf-8').encode(str)
var hash = await crypto.subtle.digest('SHA-1', buffer)
return hex(hash)
}
await sha('test')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment