Skip to content

Instantly share code, notes, and snippets.

@john-science
Created July 5, 2017 20:01
Show Gist options
  • Save john-science/b849945b349e84d207d34c1ccdcb5096 to your computer and use it in GitHub Desktop.
Save john-science/b849945b349e84d207d34c1ccdcb5096 to your computer and use it in GitHub Desktop.
Java MD5 Super Collider is Super Slow
import java.security.*;
import java.math.BigInteger;
public class md5 {
private static MessageDigest md;
private static final String charList = "0123456789abcdef";
private static SecureRandom rand = new SecureRandom();
private static final int numLoops = (int)1e6;
private static final int numSigDigs = 4;
public static void main(String[] args) throws Exception {
md = MessageDigest.getInstance("MD5");
String s = "";
String hashed = "";
int similarity = 0;
for (int i=0; i < numLoops; i++) {
s = randomHash();
hashed = getMD5(s);
similarity = compareStrings(s, hashed);
if (similarity >= numSigDigs) {
System.out.println(s + ":" + hashed);
}
}
}
private static String getMD5(String s) throws Exception {
byte[] bytesOfMessage = s.getBytes("UTF-8");
return String.format("%032x", new BigInteger(1, md.digest(bytesOfMessage)));
}
private static String randomHash(){
StringBuilder sb = new StringBuilder(32);
for(int i=0; i < 32; i++) {
sb.append(charList.charAt(rand.nextInt(16)));
}
return sb.toString();
}
private static int compareStrings(String s1, String s2) {
int i = 0;
boolean found = true;
while (found) {
if (s1.charAt(i) == s2.charAt(i)) {
i += 1;
} else {
found = false;
}
}
return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment