Skip to content

Instantly share code, notes, and snippets.

@richwednesday
Last active May 20, 2020 06:25
Show Gist options
  • Save richwednesday/d657b61069fcc9c03cdf4c793fd58414 to your computer and use it in GitHub Desktop.
Save richwednesday/d657b61069fcc9c03cdf4c793fd58414 to your computer and use it in GitHub Desktop.
hash_frontend_js
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