Skip to content

Instantly share code, notes, and snippets.

@schirrmacher
Created September 12, 2019 12:50
Show Gist options
  • Save schirrmacher/71d6833bf199ac9792f38c4df3e0619d to your computer and use it in GitHub Desktop.
Save schirrmacher/71d6833bf199ac9792f38c4df3e0619d to your computer and use it in GitHub Desktop.
Proof of Work Example
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