fluffychaos fluffychaos - 2 months ago 26
Java Question

CRC32 in Java different from pycrc

I am trying to find a CRC32 collision in Java then checking the hash with pycrc. I tried what was described in this thread, but I still can't get my implementation to match with pycrc. What am I doing wrong?

public static void print() {
Checksum h = new CRC32();
Map<Long, String> seen = new HashMap<Long, String>();

while (true) {
String s = randomString();
byte[] b = s.getBytes(StandardCharsets.UTF_8);
h.update(b, 0, b.length);
Long l = h.getValue();
if (!seen.containsKey(l)) {
seen.put(l, s);
} else {
System.out.println(s + "; " + seen.get(l));
return;
}
}
}


Edit

After some more investigation, I found that it isn't that pycrc is hashing differently from Java's implementation, but that Java is simply giving me two strings with different hashes. For example, "93C7946B05" hashes to "0xf2792761" and "323C239466" hashes to "0x59fc1818", but when Java is comparing the hashes (using the implementation below), they appear to be "equal."

Updated code:

static char[] chars = "0123456789ABCDEF".toCharArray();

public static void print() {
Checksum h = new CRC32();
String key;
Map<String, String> seen = new HashMap<String, String>();

while (true) {
String s = randomString();
byte[] b = s.getBytes(StandardCharsets.UTF_8);
h.update(b, 0, b.length);
Long l = h.getValue();
key = Long.toHexString(l);
if (!seen.containsKey(key)) {
seen.put(key, s);
} else {
System.out.println(s + "; " + seen.get(key));
return;
}
}
}

public static String randomString() {
StringBuilder sb = new StringBuilder();
Random random = new Random();
//int len = random.nextInt(32) + 1;
//for (int i = 0; i < len; i++) {
for (int i = 0; i < 10; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String output = sb.toString();
return output;
}

Answer

Your problem is that you re-use the CRC32 instance without calling h.reset();.

Therefore the CRC32 you get is not for the current string to be tested but for the concat of all strings you have tested so far.