Skip to content

Instantly share code, notes, and snippets.

@ConsciousCode
Created January 1, 2020 00:01
Show Gist options
  • Save ConsciousCode/22275277acc27addfc328a4d10bf856e to your computer and use it in GitHub Desktop.
Save ConsciousCode/22275277acc27addfc328a4d10bf856e to your computer and use it in GitHub Desktop.
'use strict';
function fromArray(arr) {
let num = 0n;
for(let x of arr) {
num <<= 8n;
num |= BigInt(x);
}
return num;
}
function toArray(num, big) {
let arr = [];
while(num) {
arr.push(Number(num%256n));
num /= 256n;
}
// Little endian deconstruction, must reverse bytes
return new Uint8Array(arr.reverse());
}
function strength(x) {
return /^(1*)/.exec(x.toString(2).padStart(256, 0))[1].length;
}
function test(t) {
let stop = false;
async function hash() {
let max = 0, n = 0;
while(!stop) {
let r = new Uint8Array(crypto.getRandomValues(new Uint32Array(4)));
let h = fromArray(new Uint8Array(await crypto.subtle.digest("SHA-256", r)));
//console.log(h.toString(2).slice(0, 20), strength(h));
if(h > max) max = h;
++n;
}
console.log(max, strength(max), n);
}
setTimeout((() => stop = true), t);
hash();
}
test(1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment