Created
May 12, 2014 21:02
-
-
Save NikolaiT/76d6294e1573d2a38fb8 to your computer and use it in GitHub Desktop.
some java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.UUID; | |
import java.util.Random; | |
/* | |
* Run and compile: | |
* | |
* javac Timer.java && java -Djava.compiler=NONE Timer | |
* | |
* Aufgabe 1. | |
* | |
* Wegen der ersten Zeile [ if(a.length != b.length) return false; ] kann man sehr schnell die | |
* Laenge des gesuchten Passwortes herausfinden, da falls die Laenge nicht stimmt, die Methode sofort | |
* (nach sehr kurzer Zeit) messbar false zurueckgibt. Also kann man schnell die Passwort laenge ermitteln. | |
* | |
* Dann geht man char fuer char vor. Man nimmt zu Beginn ein irgend ein passwort mit der gesuchten Laenge | |
* und veraendert dann das erste Char so lange, bis passwordCompare() messbar laenger laeuft als zuvor. Dazu braucht | |
* man 256 Versuche. Wenn man immer 2 Chars auf einmal veraendert (Um die Timing unterschiede zu erhoehen), muss man halt | |
* 256**2 == 65k mal testen (Kein ding in Java), so hat man bessere Messungen. | |
* | |
* Man geht solange charweise vor, bis alle Chars herausgefunden wurden. | |
*/ | |
class Timer { | |
static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
static Random rnd = new Random(); | |
public static void main(String[] args) { | |
// first just run the function once (this is somehow required :/) | |
// ignore this (maybe this is used to initialize the byte code interpreter, or JIC disabling doesn't work on my box) | |
testPwd("dummy", "blabal"); | |
// these methods calls should (theoretically) finish very quickly, because they immediately returns false (the passwords are of different length) | |
testPwd("12345678", "1234567"); | |
testPwd("12345678", "123456"); | |
testPwd("12345678", "12345"); | |
testPwd("12345678", "1234"); | |
testPwd("12345678", "123"); | |
testPwd("12345678", "12"); | |
// whereas this should (theoretically) run measurably longer, because the whole for loop is consumed | |
testPwd("12345678", "12345678"); | |
// Hence we may obtain the password length | |
// then proceed as stated in the comments above (before class declaration) | |
} | |
public static void testLong() { | |
String r1 = randomString(1000); | |
String r2 = randomString(1000); | |
testPwd(r1, r1); | |
testPwd(r1, r2); | |
} | |
public static boolean passwordCompare(char[] a, char[] b) { | |
int i; | |
if(a.length != b.length) return false; | |
for(i=0; i<a.length && a[i]==b[i]; i++); | |
return i == a.length; | |
} | |
static void testPwd(String a, String b) { | |
long start, end; | |
char[] ca, cb; | |
ca = a.toCharArray(); cb = b.toCharArray(); | |
start = System.nanoTime(); | |
passwordCompare(ca, cb); | |
end = System.nanoTime(); | |
System.out.printf("testPwd(%s, %s) used %d nano seconds\n", a, b, end-start); | |
} | |
static String randomString(int len) | |
{ | |
StringBuilder sb = new StringBuilder( len ); | |
for( int i = 0; i < len; i++ ) | |
sb.append( AB.charAt( rnd.nextInt(AB.length()) ) ); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment