Last active
May 20, 2020 06:25
-
-
Save richwednesday/d657b61069fcc9c03cdf4c793fd58414 to your computer and use it in GitHub Desktop.
hash_frontend_js
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
async function digestMessage(message) { | |
const encoder = new TextEncoder(); | |
const data = encoder.encode(message); | |
const hash = await crypto.subtle.digest('SHA-256', data); | |
return hash; | |
} | |
function toHexString(byteArray) { | |
return Array.from(byteArray, function(byte) { | |
return ('0' + (byte & 0xFF).toString(16)).slice(-2); | |
}).join('') | |
} | |
async function hashMessage(message) { | |
const digestBuffer = await digestMessage(JSON.stringify(message)); | |
const uintarray = new Uint8Array(digestBuffer); | |
return toHexString(uintarray); | |
} | |
const text = "Don't rush. We dey go up." | |
const hashed = hashMessage(text); | |
console.log(hashed); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment