-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHasher.java
100 lines (88 loc) · 3.36 KB
/
Hasher.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.Charset;
import java.util.*;
public class Hasher {
private static String original, toCompare, hashedOriginal, hashedToCompare, testBit;
private int i, matchedBits, counter;
// private double counter;
private Hashtable<String, String> hashTable;
static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
/**
* Converts a string into its hashed equivalent
*
* @param input the string to hash
* @return returns the hash of the input string
* @throws NoSuchAlgorithmException
*/
static String sha1(String input) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
/**
* Randomly generates an alphanumerical string
*
* @param len the length of the randomly generated string
* @return this returns a string of length len
*/
static String randomString(int len) {
Random rnd = new Random();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
sb.append(AB.charAt(rnd.nextInt(AB.length())));
return sb.toString();
}
/**
* Converts a randomly generated string into hash and stores it in a hash table
* Compares a randomly generated string's hash to other hashes in the table
* Makes sure that identical input strings are not matched Prints both strings
* with a matching hash of the firsts x nibbles
*
* @throws NoSuchAlgorithmException
*/
public Hasher() throws NoSuchAlgorithmException {
i = 0;
counter = 0;
matchedBits = 15;
hashTable = new Hashtable<String, String>();
Set<String> plainText = hashTable.keySet();
while (i == 0) {
toCompare = randomString(30);
hashedToCompare = sha1(toCompare);
// substrings the hash so we only store the nibbles that we need to match
hashedToCompare = hashedToCompare.substring(0, matchedBits);
counter = counter + 1;
if (counter % 10000 == 0) {
System.out.println(counter);
}
// fills the hashtable with hashes until it reaches a certain size
if (hashTable.size() < 20000000) {
hashTable.put(toCompare, hashedToCompare);
// checks if the hash generated matches a hash inside the table
} else if (hashTable.containsValue(hashedToCompare) && hashTable.containsKey(toCompare) == false) {
for (String key : plainText) {
if (hashTable.get(key).equals(hashedToCompare)) {
System.out.println("String matches found!");
System.out
.println("String 1 and 2 have " + matchedBits + " matching starting consecutive bits.");
System.out.println("String 1 = " + key);
System.out.println("String 2 = " + toCompare);
System.out.println("String 1 and 2 have the following matching bits = " + hashedToCompare);
System.out.println("String 1 Full Hash = " + sha1(key));
System.out.println("String 2 full Hash = " + sha1(toCompare));
i = 1;
break;
}
}
}
}
}
public static void main(String[] Args) throws NoSuchAlgorithmException {
Hasher test = new Hasher();
}
}