Java CRC32: not the same as CRC from C #

I need to compare files with java with the CRC32 code provided by C # script. When I calculate CRC32 with java.util.zip.CRC32, the result is completely different ...

I assume that polynomial = 0x2033 C # script does not match the one used in zip.CRC32. Is it possible to set a polynomial? Or any java class ideas for calculating CRC32 where you can define your own polynomial?

UPDATE: The problem is not polynomial. The same thing happens between C # and Java

This is my code, maybe something is wrong with the way I read the file?

package com.mine.digits.internal.contentupdater; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.CRC32; public class CRC { public static String doConvert32(File file) { byte[] bytes = readBytesFromFile(file); // readFromFile(file).getBytes(); CRC32 x = new CRC32(); x.update(bytes); return (Long.toHexString(x.getValue())).toUpperCase(); } /** Read the contents of the given file. */ private static byte[] readBytesFromFile(File file) { try { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { // File is too large } byte[] bytes = new byte[(int)length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { System.out.println("Could not completely read file " + file.getName()); } // Close the input stream and return bytes is.close(); return bytes; } catch (IOException e) { System.out.println("IOException " + file.getName()); return null; } } } 

Thanks a lot Frank

+2
source share
4 answers

Solved by copying code from C # and converting it to a Java class ...

Thus, now both use the same code, but had to make minor changes for the unsigned and lt differences; > with bytes.

0
source

Standard (IEEE) CRC32 polynomial 0x04C11DB7 , which corresponds to:

 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 

This is what java.util.zip.CRC32 uses. Not sure about the C # script you mention ...

You may find this piece of code useful:

+3
source

CRC-32 is a specific CRC variant in accordance with IEEE 802.3 and uses the polynomial 0x04C11DB7. If your C # library uses the polynomial 0x2033, this is / not / an implementation of CRC-32.

If you need Java code to compute arbitrary CRC options, googling "java crc" will give you some examples.

+1
source

1 + x + x ^ 2 + x ^ 4 + x ^ 5 + x ^ 7 + x ^ 8 + x ^ 10 + x ^ 11 + x ^ 12 + x ^ 16 + x ^ 22 + x ^ 23 + x ^ 26 (0x04C11DB7) Java uses the above polynomial to calculate CRC 32 and is different from the IEEE 802.3 standard, which additionally has 32-bit x power.

0
source

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


All Articles