Java CRC32: not the same as CRC from C#

别说谁变了你拦得住时间么 提交于 2020-01-02 10:17:10

问题


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

My guess is that the polynom = 0x2033 of the C# script is not the same as used in zip.CRC32. Is it possible to set the polynom ? Or any ideas of a java-class for calculating a CRC32 where you can define your own polynom?

UPDATE: problem is not the polymnom. This is the same between C# and Java

This is my code, maybe something is wrong in 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


回答1:


The standard (IEEE) CRC32 polynomial is 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. Don't know about the C# script you mention...

You can find this code snippet useful:

  • http://www.cs.princeton.edu/introcs/51data/CRC32.java.html



回答2:


CRC-32 is a specific CRC variant according to IEEE 802.3 and is using the polynom 0x04C11DB7. If your C# library is using the polynom 0x2033, it is /not/ an implementation of CRC-32.

If you need Java code to calculate arbitrary CRC variants, googling "java crc" will give you several examples.




回答3:


Solved by copying the code from C# and convert it to a Java-class...

So now both use the same code, only had to do some minor changes for unsigned <> signed bytes differences.




回答4:


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 for CRC 32 calculation and is different from IEEE 802.3 standard which has a 32 bit power of x in addition.



来源:https://stackoverflow.com/questions/4344190/java-crc32-not-the-same-as-crc-from-c-sharp

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