diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-19 12:03:36 +0100 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-19 12:03:36 +0100 |
| commit | cc56343a75297fa39b3d215804b6145ef00ff1ed (patch) | |
| tree | f9b46670965f7fea39ec7bf58888a130caecf8f2 /ws2015 | |
| parent | 171df5103570946d4e1ff015dee9ed657036e94a (diff) | |
| download | uni-cc56343a75297fa39b3d215804b6145ef00ff1ed.tar uni-cc56343a75297fa39b3d215804b6145ef00ff1ed.tar.gz uni-cc56343a75297fa39b3d215804b6145ef00ff1ed.tar.bz2 uni-cc56343a75297fa39b3d215804b6145ef00ff1ed.tar.xz uni-cc56343a75297fa39b3d215804b6145ef00ff1ed.zip | |
EiP 10.2
Diffstat (limited to 'ws2015')
| -rw-r--r-- | ws2015/eip/blaetter/10/2/PasswordCracker.java | 36 | ||||
| -rw-r--r-- | ws2015/eip/blaetter/10/2/PasswordCrackerThread.java | 29 | ||||
| -rw-r--r-- | ws2015/eip/blaetter/10/2/SHA.java | 28 | ||||
| -rw-r--r-- | ws2015/eip/blaetter/10/2/Synchronizer.java | 22 | ||||
| -rw-r--r-- | ws2015/eip/blaetter/10/manifest | 2 |
5 files changed, 117 insertions, 0 deletions
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 @@ | |||
| 1 | public class PasswordCracker { | ||
| 2 | |||
| 3 | // dies ist der laut Angabe bekannte Hashwert des Passworts | ||
| 4 | public static final String HASH = "ef937752f7348a964445f60857009f881983a6af"; | ||
| 5 | |||
| 6 | public static void main(String[] args) { | ||
| 7 | int numProcs = Runtime.getRuntime().availableProcessors(); | ||
| 8 | Thread[] workers = new Thread[numProcs]; | ||
| 9 | |||
| 10 | Synchronizer sync = new Synchronizer(HASH); | ||
| 11 | |||
| 12 | for (int i = 0; i < numProcs; i++) { | ||
| 13 | workers[i] = new Thread(new PasswordCrackerThread(sync, rangeBound(numProcs, i), rangeBound(numProcs, i + 1) - 1)); | ||
| 14 | workers[i].start(); | ||
| 15 | } | ||
| 16 | |||
| 17 | synchronized (sync) { | ||
| 18 | while (! sync.hasSolution()) { | ||
| 19 | try { | ||
| 20 | sync.wait(); | ||
| 21 | } catch (InterruptedException e) {} | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | for (Thread w : workers) | ||
| 26 | w.interrupt(); | ||
| 27 | |||
| 28 | System.out.println("Guessed this password: " + sync.getSolution()); | ||
| 29 | System.exit(0); | ||
| 30 | } | ||
| 31 | |||
| 32 | public static long rangeBound(int n, int i) { | ||
| 33 | long upperBound = (long) 1e8; | ||
| 34 | return n == i ? upperBound + 1 : (upperBound / n) * i; | ||
| 35 | } | ||
| 36 | } | ||
diff --git a/ws2015/eip/blaetter/10/2/PasswordCrackerThread.java b/ws2015/eip/blaetter/10/2/PasswordCrackerThread.java new file mode 100644 index 0000000..dcc0656 --- /dev/null +++ b/ws2015/eip/blaetter/10/2/PasswordCrackerThread.java | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | import java.util.Formatter; | ||
| 2 | |||
| 3 | public class PasswordCrackerThread implements Runnable { | ||
| 4 | |||
| 5 | public final long rangeStart; | ||
| 6 | public final long rangeEnd; | ||
| 7 | |||
| 8 | public final Synchronizer sync; | ||
| 9 | |||
| 10 | public PasswordCrackerThread(Synchronizer s, long start, long end) | ||
| 11 | { | ||
| 12 | sync = s; | ||
| 13 | rangeStart = start; | ||
| 14 | rangeEnd = end; | ||
| 15 | } | ||
| 16 | |||
| 17 | public void run() { | ||
| 18 | for (long i = rangeStart; i <= rangeEnd; i++) { | ||
| 19 | String guess = new Formatter().format("%8d", i).toString(); | ||
| 20 | String digest = SHA.encrypt(guess); | ||
| 21 | if (digest.equals(sync.targetHash)) { | ||
| 22 | synchronized (sync) { | ||
| 23 | sync.setSolution(i); | ||
| 24 | break; | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | ||
diff --git a/ws2015/eip/blaetter/10/2/SHA.java b/ws2015/eip/blaetter/10/2/SHA.java new file mode 100644 index 0000000..ad40f99 --- /dev/null +++ b/ws2015/eip/blaetter/10/2/SHA.java | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | import java.security.MessageDigest; | ||
| 2 | import java.security.NoSuchAlgorithmException; | ||
| 3 | import java.util.Formatter; | ||
| 4 | |||
| 5 | public class SHA { | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Gibt den Hashwert des Parameters zurueck - die Implementierung dieser Methode | ||
| 9 | * ist fuer diese Aufgabe *NICHT* relevant, braucht nicht weiter beachtet zu werden | ||
| 10 | * @param text auf den der Hashalgorithmus angewendet werden soll | ||
| 11 | * @return den Hashwert | ||
| 12 | */ | ||
| 13 | public static String encrypt(String text) { | ||
| 14 | try { | ||
| 15 | MessageDigest md = MessageDigest.getInstance("SHA"); | ||
| 16 | md.update(text.getBytes()); | ||
| 17 | byte[] digest = md.digest(); | ||
| 18 | |||
| 19 | Formatter formatter = new Formatter(); | ||
| 20 | for (byte d: digest) | ||
| 21 | formatter.format("%02x", 0xFF & d); | ||
| 22 | return formatter.toString(); | ||
| 23 | } catch (NoSuchAlgorithmException e) { | ||
| 24 | e.printStackTrace(); | ||
| 25 | return null; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } \ No newline at end of file | ||
diff --git a/ws2015/eip/blaetter/10/2/Synchronizer.java b/ws2015/eip/blaetter/10/2/Synchronizer.java new file mode 100644 index 0000000..5efb127 --- /dev/null +++ b/ws2015/eip/blaetter/10/2/Synchronizer.java | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | public class Synchronizer { | ||
| 2 | public final String targetHash; | ||
| 3 | private Long solution = null; | ||
| 4 | |||
| 5 | public Synchronizer(String hash) { targetHash = hash; } | ||
| 6 | |||
| 7 | public synchronized void setSolution(long password) | ||
| 8 | { | ||
| 9 | solution = password; | ||
| 10 | this.notifyAll(); | ||
| 11 | } | ||
| 12 | |||
| 13 | public synchronized long getSolution() | ||
| 14 | { | ||
| 15 | return solution; | ||
| 16 | } | ||
| 17 | |||
| 18 | public synchronized boolean hasSolution() | ||
| 19 | { | ||
| 20 | return (solution != null); | ||
| 21 | } | ||
| 22 | } | ||
diff --git a/ws2015/eip/blaetter/10/manifest b/ws2015/eip/blaetter/10/manifest new file mode 100644 index 0000000..7a754f4 --- /dev/null +++ b/ws2015/eip/blaetter/10/manifest | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | 1 | ||
| 2 | 2 \ No newline at end of file | ||
