Skip to content

Instantly share code, notes, and snippets.

@riston
Created February 16, 2014 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riston/9040092 to your computer and use it in GitHub Desktop.
Save riston/9040092 to your computer and use it in GitHub Desktop.
Hash finder for hash challange http://www.h11e.com/
var crypto = require('crypto'),
cuid = require('cuid'),
string,
zeros = 0,
currentHash = '',
currentZeros = 0,
hash;
function findZeros(hash) {
var chars = hash.split(''),
zeros = 0,
i;
for (i = 0; i < chars.length; i++) {
if (chars[i] === '0') {
zeros++;
} else {
break;
}
}
return zeros;
}
/**
* Why you use interval here why not while (true) ?
* Because the computer I use is not meant for this mainly
*
* You know that in node you got one thread ?
* Yes you can use cluster module for creating something fancier for this
*
* @return {Number} interval id
*/
var id = setInterval(function () {
string = cuid();
hash = crypto.createHash('sha512').update(string).digest('hex');
currentZeros = findZeros(hash);
if (currentZeros > zeros) {
currentHash = hash;
zeros = currentZeros;
console.log("Better hash found " + hash + " - " + string);
}
}, 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment