user3371785
user3371785

Reputation: 91

CRC32 not calculating the right checksum?

I'm trying to calculate the crc32 checksum of a file, but it seems that my code isn't calculating it right.

def crc32_checksum(directory):
    file = open(directory, "r").read()
    file_checksum = str(binascii.crc32(file) & 0xffffffff)
    return file_checksum

Obviously that will return the decimal checksum (right?), but when I convert it to hex, it doesn't seem to match up (I'm using one of those decimal to hex websites).

I get this as the Python return: 2370036543 (8D43E33F) and checksum that the DigitalVolcano HashTool 1.1 I downloaded returns this: (300654116) 11eb9e24.

I'm positive that the hashtool is correct as I've done comparing of checksums using it (and they matched).

I'm running Windows.

Upvotes: 1

Views: 1595

Answers (1)

Mark Adler
Mark Adler

Reputation: 112547

You don't say what system you are doing this on, but you may need to open with "rb" instead of "r" to assure that there are no end-of-line translations on the read data. It is good practice to put that in anyway for portability and to document that you are considering the input to be binary data.

Upvotes: 3

Related Questions