-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinerThread.java
More file actions
68 lines (63 loc) · 2.18 KB
/
Copy pathMinerThread.java
File metadata and controls
68 lines (63 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
public class MinerThread extends Thread
{
private String challenge = "";
public String messageToMaster = "";
public String hashOfSolution = "";
public boolean shouldContinue = true;
public double hashesPerSecond = 0;
public MinerThread(String challenge)
{
this.challenge = challenge;
}
public void run()
{
try
{
String prefix = challenge.split(",")[0];
int numLoops = Integer.parseInt(challenge.split(",")[1]);
int incrementMe = new Random().nextInt(Integer.MAX_VALUE);
//shouldContinue set to false by master when work is old/stale.
long lastTimestamp = System.currentTimeMillis();
int lastIncrementMe = incrementMe;
while (shouldContinue)
{
if(SHA256(prefix + incrementMe, numLoops).substring(0, 6).equals("000000"))
{
hashOfSolution = SHA256(prefix + incrementMe, numLoops);
messageToMaster = prefix + incrementMe;
shouldContinue = false;
}
incrementMe++;
if (incrementMe - 10000 == lastIncrementMe)
{
lastIncrementMe = incrementMe;
double secondsPassed = ((double)System.currentTimeMillis() - (double)lastTimestamp)/1000;
hashesPerSecond = 10000/secondsPassed;
lastTimestamp = System.currentTimeMillis();
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public String SHA256(String toHash, int rounds)
{
try
{
MessageDigest md = MessageDigest.getInstance("SHA-256");
String hash = DatatypeConverter.printHexBinary(md.digest(toHash.getBytes("UTF-8")));
for (int i = 1; i < rounds; i++)
{
hash = DatatypeConverter.printHexBinary(md.digest(hash.getBytes("UTF-8")));
}
return hash;
} catch (Exception e)
{
return null;
}
}
}