-
Notifications
You must be signed in to change notification settings - Fork 48
/
NonceTest.java
49 lines (32 loc) · 1.07 KB
/
NonceTest.java
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
package nonce;
import static org.junit.Assert.*;
import org.junit.Test;
import helpers.SHA256;
public class NonceTest {
@Test
public void test() {
String message = "Keyhole Software";
System.out.println("Message: " + message);
String hashValue = SHA256.generateHash(message);
System.out.println(String.format("Hash: %s", hashValue));
String nonceKey = "12345";
// E.g. "00000" :
String zeroGoal = new String(new char[nonceKey.length()]).replace("\0", "0");
long nonce = 0;
boolean isNonceFound = false;
String nonceHash = "";
long start = System.currentTimeMillis();
while (!isNonceFound) {
nonceHash = SHA256.generateHash(message + nonce);
isNonceFound = nonceHash.substring(0, nonceKey.length()).equals(zeroGoal);
if (!isNonceFound) {
nonce++;
}
}
long ms = System.currentTimeMillis() - start;
System.out.println(String.format("Nonce: %d", nonce));
System.out.println(String.format("Nonce Hash: %s", nonceHash));
System.out.println(String.format("Nonce Search Time: %s ms", ms));
assertTrue(isNonceFound);
}
}