Skip to content

Instantly share code, notes, and snippets.

@tech4him1
Last active April 18, 2018 00:44
Show Gist options
  • Save tech4him1/09244db980a7e9dd4ddde2b70fe95245 to your computer and use it in GitHub Desktop.
Save tech4him1/09244db980a7e9dd4ddde2b70fe95245 to your computer and use it in GitHub Desktop.
Generate random SHA256 look-alike.
const hashlen = 256;
const hashbase = 16;
const randomNums = new Uint32Array(hashlen / 32);
window.crypto.getRandomValues(randomNums);
const padNum = (num, base) => {
const padLen = (32 / Math.sqrt(base));
const str = num.toString(base);
return (('0' * padLen) + str).slice(-padLen);
}
const randomSHA256 = randomNums.reduce((agg, val) => (agg + padNum(val, hashbase)), '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment