From cc56343a75297fa39b3d215804b6145ef00ff1ed Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 19 Jan 2016 12:03:36 +0100 Subject: EiP 10.2 --- ws2015/eip/blaetter/10/2/PasswordCracker.java | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ws2015/eip/blaetter/10/2/PasswordCracker.java (limited to 'ws2015/eip/blaetter/10/2/PasswordCracker.java') diff --git a/ws2015/eip/blaetter/10/2/PasswordCracker.java b/ws2015/eip/blaetter/10/2/PasswordCracker.java new file mode 100644 index 0000000..0c7c094 --- /dev/null +++ b/ws2015/eip/blaetter/10/2/PasswordCracker.java @@ -0,0 +1,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; + } +} -- cgit v1.2.3