CRC-32 implementation in java.util.zip.CRC32

What CRC-32 algorithm is used in Java CRC-32 class? There are no details in the java document. What is the polynomial used and the initial value for the calculation?

+4
source share
4 answers

According to source :

Calculates the CRC32 data checksum of the data stream. The actual CRC32 algorithm is described in RFC 1952 (GZIP file format specification version 4.3). It can be used to get CRC32 downstream if used with trusted input / output streams.

RFC1952 can be found here , but is a pretty technical read.

The initial value for CRC is 0xFFFFFFFF , and the CRC table is created the first time the class is run on the virtual machine.

+4
source

CRC-32 is specified in the package documentation for java.util.zip, which must be specified in RFC 1952 . RFC 1952 defines CRC32 as specified in ISO 3309, which I could not find a free copy to link you to. However, RFC 1952 also indicates that the same implementation is indicated in section 8.1.1.6.2 of ITU-T Rec. V.42 .

In particular, the polynomial used has the form

 x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1 
+5
source

Using some tools from the Internet ( http://www.sunshine2k.de/coding/javascript/crc/crc_js.html ) I found a combination of CRC32 parameters that gives the same results as those obtained from Java:

  • The input is reflected, the result is reflected.
  • Polynomial: 0x04C11DB7.
  • Initial value: 0xFFFFFFFF.
  • Final XOR: 0xFFFFFFFF
+1
source

The currently accepted answer is incorrect.

The initial value for the Java class, CRC32 is 0, not 0xFFFFFFFF, as can be seen in the source code for the reset function:

 /** * Resets CRC-32 to initial value. */ public void reset() { crc = 0; } 

https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/zip/CRC32.java#L81

I did a quick brute force search, and it turns out that updating the CRC with a value of 0xFFFFFFFF actually give the same value. Therefore, if you want the CRC32 algorithm to have an initial value of 0xFFFFFFFF , simply do:

  CRC32 crc = new CRC32(); // Set the initial value to 0xFFFFFFFF crc.update(new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); System.out.println("CRC: " + crc.getValue()); // prints 4294967295, which is 0xFFFFFFFF 
0
source

Source: https://habr.com/ru/post/1392952/


All Articles