Created
September 12, 2019 12:50
-
-
Save schirrmacher/71d6833bf199ac9792f38c4df3e0619d to your computer and use it in GitHub Desktop.
Proof of Work Example
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
const crypto = require("crypto"); | |
// PROOF OF WORK EXAMPLE | |
const RANDOM_SIZE = 32; | |
const HARDNESS = 6; | |
const hardnessPrefix = "0".repeat(HARDNESS); | |
const start = new Date().getTime(); | |
let i = 0; | |
let random; | |
let result; | |
do { | |
random = crypto.randomBytes(RANDOM_SIZE); | |
result = crypto | |
.createHash("sha256") | |
.update(random.toString("hex")) | |
.digest("hex"); | |
i++; | |
} while (!result.toString("hex").startsWith(hardnessPrefix)); | |
const end = new Date().getTime(); | |
console.log(`${i} iterations (${end - start}):`); | |
console.log(random.toString("hex")); | |
console.log(result.toString("hex")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment