Created
April 24, 2019 06:36
-
-
Save Silur/0a39cc40c118084124dab39e28ba6f2b to your computer and use it in GitHub Desktop.
stop the annoying captcha with hashcash
This file contains hidden or 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 sha256 = require('hash.js').sha256 | |
module.exports = { | |
function pow(input, diff) { | |
let candidate = 0; | |
let nonce = 0; | |
while(true) { | |
nonce = Math.random() | |
candidate = sha256().update(input + nonce.toString()).dig | |
est('hex') | |
if(parseInt(candidate, 16) < diff) break | |
} | |
console.log('proof: ' + candidate) | |
return nonce.toString() | |
} | |
function verify(input, diff, proof) { | |
if(parseInt(sha256().update(input+proof).digest('hex'), 16) | |
< diff) return true | |
return false | |
} | |
} | |
/* test | |
const diff = 22085588309729804119791218759286481447843548710945 | |
2369765200775161577472 | |
const proof = pow('this is my kung fu', diff) | |
console.log(verify('this is my kung fu', diff, proof)) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment