Show More
|
1 | NO CONTENT: modified file, binary diff hidden |
@@ -1,54 +1,54 b'' | |||
|
1 | 1 | var POW_COMPUTING_TIMEOUT = 2; |
|
2 | 2 | var POW_HASH_LENGTH = 16; |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | function computeHash(iteration, guess, target, payload, timestamp, hasher) { |
|
6 | 6 | iteration += 1; |
|
7 | 7 | var hash = hasher(payload + iteration).toString(); |
|
8 | 8 | guess = hash.substring(0, POW_HASH_LENGTH); |
|
9 | 9 | |
|
10 | 10 | if (guess <= target) { |
|
11 | console.log("Iteration: ", iteration); | |
|
12 | console.log("Guess: ", guess); | |
|
13 | console.log("Target: ", target); | |
|
11 | //console.log("Iteration: ", iteration); | |
|
12 | //console.log("Guess: ", guess); | |
|
13 | //console.log("Target: ", target); | |
|
14 | 14 | |
|
15 | 15 | var data = { |
|
16 | 16 | iteration: iteration, |
|
17 | 17 | timestamp: timestamp, |
|
18 | 18 | guess: guess |
|
19 | 19 | }; |
|
20 | 20 | self.postMessage(data); |
|
21 | 21 | } else { |
|
22 | 22 | //console.log("Iteration: ", iteration); |
|
23 | 23 | //console.log("Guess: ", guess); |
|
24 | 24 | //console.log("Target: ", target); |
|
25 | 25 | |
|
26 | 26 | setTimeout(function() { |
|
27 | 27 | computeHash(iteration, guess, target, payload, timestamp, hasher); |
|
28 | 28 | }, POW_COMPUTING_TIMEOUT); |
|
29 | 29 | } |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | function doWork(message, hasher, difficulty) { |
|
33 | 33 | var timestamp = Date.now(); |
|
34 | 34 | var iteration = 0; |
|
35 | 35 | var payload = timestamp + message; |
|
36 | 36 | |
|
37 | 37 | var target = parseInt(Math.pow(2, POW_HASH_LENGTH * 3) / difficulty).toString(); |
|
38 | 38 | while (target.length < POW_HASH_LENGTH) { |
|
39 | 39 | target = '0' + target; |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | var guess = target + '0'; |
|
43 | 43 | |
|
44 | 44 | setTimeout(function() { |
|
45 | 45 | computeHash(iteration, guess, target, payload, timestamp, hasher); |
|
46 | 46 | }, POW_COMPUTING_TIMEOUT); |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | self.onmessage = function(e) { |
|
50 | 50 | var difficulty = e.data.difficulty; |
|
51 | 51 | importScripts(e.data.hasher); |
|
52 | 52 | var hasher = CryptoJS.SHA256; |
|
53 | 53 | self.doWork(e.data.msg, hasher, difficulty); |
|
54 | 54 | }; |
General Comments 0
You need to be logged in to leave comments.
Login now