-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2.java
More file actions
81 lines (64 loc) · 2.42 KB
/
Copy pathPart2.java
File metadata and controls
81 lines (64 loc) · 2.42 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
69
70
71
72
73
74
75
76
77
78
79
80
81
package y2016.d14;
import org.apache.commons.codec.binary.Hex;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Part2 {
private static MessageDigest md5;
private static Pattern pattern1 = Pattern.compile("([a-f0-9])(\\1{2})");
// private static String salt = "abc";
private static String salt = "cuanljph";
private static HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
public static void main(String[] args) throws NoSuchAlgorithmException {
md5 = MessageDigest.getInstance("MD5");
HashSet<String> keys = new HashSet<String>();
int count = 0;
while (keys.size() < 64) {
String hash = hashOrDie(count);
String result3 = getCharOfSequenceOfThree(hash);
if (null != result3) {
String hash2;
for (int i = count + 1; i < count + 1000; i++) {
hash2 = hashOrDie(i);
if (hasSequenceOfFive(hash2, result3)) {
System.out.print("three (" + result3 + "):" + hash + " @ " + count);
System.out.print(" five (" + result3 + "):" + hash2 + " @ " + i + " | ");
keys.add(hash);
System.out.println(keys.size());
break;
}
}
}
++count;
}
System.out.println(count-1);
}
private static String hashOrDie(int count) {
if (hashMap.containsKey(count)) {
return hashMap.get(count);
}
String hash = getMd5Hex(salt + count);
for (int i = 0; i < 2016; i++ ) {
hash = getMd5Hex(hash);
}
hashMap.put(count, hash);
return hash;
}
private static String getCharOfSequenceOfThree(String hash) {
Matcher matcher = pattern1.matcher(hash);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
private static boolean hasSequenceOfFive(String hash, String character) {
return hash.matches(".*" + character + "{5}.*");
}
private static String getMd5Hex(String input) {
byte[] hashInBytes = md5.digest(input.getBytes());
return Hex.encodeHexString(hashInBytes);
}
}