1
我想在Java中找到CRC32冲突,然后用pycrc检查散列。我尝试了this线程中描述的内容,但仍然无法使我的实现与pycrc匹配。我究竟做错了什么?Java中的CRC32不同于pycrc
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;
}
}
}
编辑
经过一番更多的研究,我发现,它不是pycrc从Java的实现散列不同,而且Java的只是给我两串不同的哈希值。例如,“93C7946B05”散列为“0xf2792761”和“323C239466”散列为“0x59fc1818”,但是当Java比较散列(使用下面的实现)时,它们看起来是“相等的”。
更新代码:
static char[] chars = "ABCDEF".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;
}
请举例说明。 –
检查随机数据上的冲突如何证明任何事情?更不用说不同意其他一些实现? – EJP
@EJP我只是想找到一个碰撞。你认为使用随机数据不是一个好办法吗?我应该更系统地做这件事吗? – fluffychaos