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