##// END OF EJS Templates
Merged in bodqhrohro/neboard-1/bodqhrohro/pillow-now-requires-zlib-too-1522164393285 (pull request #27)...
Merged in bodqhrohro/neboard-1/bodqhrohro/pillow-now-requires-zlib-too-1522164393285 (pull request #27) Pillow now requires zlib too Approved-by: Bohdan Horbyeshko <bodqhrohro@yandex.ru>

File last commit:

r1468:14229bdf default
r2097:5ab4630b merge default
Show More
proof_of_work.js
66 lines | 1.7 KiB | application/javascript | JavascriptLexer
var POW_COMPUTING_TIMEOUT = 2;
var POW_HASH_LENGTH = 16;
var hasher = null;
function computeHash(iteration, guess, target, payload, timestamp, port) {
iteration += 1;
var hash = hasher(payload + iteration).toString();
guess = hash.substring(0, POW_HASH_LENGTH);
if (guess <= target) {
//console.log("Iteration: ", iteration);
//console.log("Guess: ", guess);
//console.log("Target: ", target);
var data = {
iteration: iteration,
timestamp: timestamp,
guess: guess
};
port.postMessage(data);
} else {
//console.log("Iteration: ", iteration);
//console.log("Guess: ", guess);
//console.log("Target: ", target);
setTimeout(function() {
computeHash(iteration, guess, target, payload, timestamp, port);
}, POW_COMPUTING_TIMEOUT);
}
}
function doWork(message, difficulty, port) {
var timestamp = Date.now();
var iteration = 0;
var payload = timestamp + message;
var target = parseInt(Math.pow(2, POW_HASH_LENGTH * 3) / difficulty).toString();
while (target.length < POW_HASH_LENGTH) {
target = '0' + target;
}
var guess = target + '0';
setTimeout(function() {
computeHash(iteration, guess, target, payload, timestamp, port);
}, POW_COMPUTING_TIMEOUT);
}
onconnect = function(e) {
var port = e.ports[0];
port.start();
port.onmessage = function(e) {
var difficulty = e.data.difficulty;
if (hasher == null) {
importScripts(e.data.hasher);
hasher = CryptoJS.SHA256;
}
self.doWork(e.data.msg, difficulty, port);
};
}