Java compatible cksum function

♀尐吖头ヾ 提交于 2019-11-29 19:57:33

问题


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 ?


回答1:


Jacksum: http://www.jonelo.de/java/jacksum/index.html

cksum         algorithm:   POSIX 1003.2 CRC algorithm
              length:      32 bits
              type:        crc
              since:       Jacksum 1.0.0
              comment:     - under BeOS    it is /bin/cksum
                           - under FreeBSD it is /usr/bin/cksum
                           - under HP-UX   it is /usr/bin/cksum and
                             /usr/bin/sum -p
                           - under IBM AIX it is /usr/bin/cksum
                           - under Linux   it is /usr/bin/cksum 

It's open source.




回答2:


Have you tried the CRC32 class?

http://download.oracle.com/javase/7/docs/api/java/util/zip/CRC32.html

This is the crc 32 which gzip uses.




回答3:


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.



来源:https://stackoverflow.com/questions/7746476/java-compatible-cksum-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!