summaryrefslogtreecommitdiff
path: root/ws2015/eip/blaetter/10/2/PasswordCracker.java
blob: 0c7c0947864870cf7b0b2fc6490936e00635f2dc (plain)
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
public class PasswordCracker {
  
    // dies ist der laut Angabe bekannte Hashwert des Passworts
    public static final String HASH = "ef937752f7348a964445f60857009f881983a6af";
    
    public static void main(String[] args) {
	int numProcs = Runtime.getRuntime().availableProcessors();
	Thread[] workers = new Thread[numProcs];

	Synchronizer sync = new Synchronizer(HASH);

	for (int i = 0; i < numProcs; i++) {
	    workers[i] = new Thread(new PasswordCrackerThread(sync, rangeBound(numProcs, i), rangeBound(numProcs, i + 1) - 1));
	    workers[i].start();
	}

	synchronized (sync) {
	    while (! sync.hasSolution()) {
		try {
		    sync.wait();
		} catch (InterruptedException e) {}
	    }
	}

	for (Thread w : workers)
	    w.interrupt();

	System.out.println("Guessed this password: " + sync.getSolution());
	System.exit(0);
    }

    public static long rangeBound(int n, int i) {
	long upperBound = (long) 1e8;
	return n == i ? upperBound + 1 : (upperBound / n) * i;
    }
}