Created
July 10, 2019 22:15
-
-
Save jaimegmx8/c5e7512f1b94ff41abda377fdd64c937 to your computer and use it in GitHub Desktop.
Javascript native sha1 hex
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
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