2016-09-23 35 views
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; 
    } 
+0

请举例说明。 –

+0

检查随机数据上的冲突如何证明任何事情?更不用说不同意其他一些实现? – EJP

+0

@EJP我只是想找到一个碰撞。你认为使用随机数据不是一个好办法吗?我应该更系统地做这件事吗? – fluffychaos

回答

0

你的问题是,你再使用CRC32实例,而不调用h.reset();

因此,您得到的CRC32不是用于当前测试的字符串,而是用于目前测试的所有字符串的concat。