Created
September 26, 2022 14:52
-
-
Save angrymouse/c2e488616baae0ad01907975eab7eca0 to your computer and use it in GitHub Desktop.
Hash function heavyed by Collatz Conjecture problem
This file contains 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
let crypto = require("crypto"); | |
let zlib = require("zlib"); | |
let timeNow = Date.now(); | |
let data = crypto.randomBytes(128); | |
console.log("Data: ", data.toString("hex")); | |
let sha512hash = crypto.createHash("sha512").update(data).digest(); | |
let currentNum = BigInt("0x" + sha512hash.toString("hex")); | |
let numbers = []; | |
while (currentNum != 1n) { | |
if (currentNum % 2n == 0n) { | |
currentNum = currentNum / 2n; | |
} else { | |
currentNum = currentNum * 3n + 1n; | |
} | |
let algo = [ | |
"sha256", | |
"whirlpool", | |
"shake256", | |
"ripemd", | |
"rmd160", | |
"blake2b512", | |
"blake2s256", | |
"SM3", | |
"SHA384", | |
"MD5", | |
].find((al, ali) => currentNum.toString().endsWith(ali)); | |
numbers.push(mine(Buffer.from(currentNum.toString("16"), "hex"), algo, 1)); | |
} | |
console.log("Took " + (Date.now() - timeNow) / 1000 + "s"); | |
console.log( | |
"Hash", | |
crypto.createHash("shake256").update(Buffer.concat(numbers)).digest("hex") | |
); | |
function mine(data, algo, difficulty) { | |
let hash = ""; | |
let nonce = 0xff; | |
while (!hash.startsWith("0".repeat(difficulty))) { | |
hash = crypto | |
.createHash(algo) | |
.update(Buffer.concat([data, Buffer.from(nonce.toString("16"), "hex")])) | |
.digest("hex"); | |
nonce += 0x10; | |
} | |
return Buffer.from(hash); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment