Java compatible cksum function

前端 未结 3 1636
温柔的废话
温柔的废话 2021-01-02 13:52

Is there any library/code in Java to calculate the 32-bit CRC of a stream of bytes in a way thats consistent with the cksum command in unix ?

3条回答
  •  Happy的楠姐
    2021-01-02 14:33

    Carlos Rendon's statement, "I can verify that Java's CRC32 is NOT the same as /usr/bin/cksum", is incorrect.

    As Peter Lawrey mentioned, you can use Java's CRC32 directly to get the same checksum as Unix/Linux cksum.

    The correct way to do it is:

    java.util.zip.CRC32 x = new java.util.zip.CRC32();
    x.update(bytes);
    StdOut.println("CRC32 (via Java's library)     = " + Long.toHexString(x.getValue()));
    

    Source: http://introcs.cs.princeton.edu/java/61data/CRC32.java.html

    The default CRC used is based on the polynomial used for CRC error checking in the networking standard ISO/IEC 8802-3:1989.

提交回复
热议问题