-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecursiveHasher.java
43 lines (39 loc) · 1.6 KB
/
RecursiveHasher.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
import java.util.concurrent.RecursiveAction;
import org.apache.commons.codec.digest.DigestUtils;
public class RecursiveHasher extends RecursiveAction {
private Dehasher dehasher; // Pointer to the Dehasher that started the action.
private int length; // The remaining levels that we need to generate.
private String password = ""; // The password we've assembled so far.
public RecursiveHasher(Dehasher dehasher, int length) {
// Called by the Dehasher to create the root.
// The root always has a blank password.
this.dehasher = dehasher;
this.length = length;
}
private RecursiveHasher(Dehasher dehasher, int length, String password) {
// Called by parent RecursiveHashers to create children.
this(dehasher, length);
this.password = password;
}
@Override
public void compute() {
if (length == 0) { // Are we done making the password?
String hash = DigestUtils.sha1Hex(password);
try {
dehasher.put(new HashedPassword(password, hash));
// Send the hashed pair to the Dehasher that called the root action.
} catch(InterruptedException e) {
e.printStackTrace();
// This should not happen (if it does, we'll debug).
}
} else { // We need more children to help compute more passwords.
// Add another character and pass the new password to a child (several times).
// This should fork a new child for each character in the character set.
for(int i = 0; i < dehasher.CHAR_SET.length; i++) {
new RecursiveHasher(dehasher, length - 1,
password + dehasher.CHAR_SET[i]).fork();
// Children process asynchronously.
}
}
}
}