I want to calculate the CRC of file and get output like: E45A12AC
. Here's my code:
#!/usr/bin/env python import os, sys import zlib def crc(fileName): fd = open(fileName,"rb") content = fd.readlines() fd.close() for eachLine in content: zlib.crc32(eachLine) for eachFile in sys.argv[1:]: crc(eachFile)
This calculates the CRC for each line, but its output (e.g. -1767935985
) is not what I want.
Hashlib works the way I want, but it computes the md5:
import hashlib m = hashlib.md5() for line in open('data.txt', 'rb'): m.update(line) print m.hexdigest()
Is it possible to get something similar using zlib.crc32
?
Right-click the file you wish to get the CRC-32 for. A context menu appears. Select the CRC SHA submenu entry. Select any of the available algorithms: CRC-32, CRC-64, SHA-1 or SHA-256 to calculate the respective checksum, or select "*" to calculate all of them and additionally BLAKE2sp.
crc32() method, we can compute the checksum for crc32 (Cyclic Redundancy Check) to a particular data. It will give 32-bit integer value as a result by using zlib. crc32() method. Return : Return the unsigned 32-bit checksum integer.
A CRC is pretty simple; you take a polynomial represented as bits and the data, and divide the polynomial into the data (or you represent the data as a polynomial and do the same thing). The remainder, which is between 0 and the polynomial is the CRC.
crc32 computes CRC-32 on binary text data. By definition, CRC-32 refers to the 32-bit checksum of any piece of data. This method is used to compute 32-bit checksum of provided data. This algorithm is not a general hash algorithm.
A little more compact and optimized code
def crc(fileName): prev = 0 for eachLine in open(fileName,"rb"): prev = zlib.crc32(eachLine, prev) return "%X"%(prev & 0xFFFFFFFF)
PS2: Old PS is deprecated - therefore deleted -, because of the suggestion in the comment. Thank you. I don't get, how I missed this, but it was really good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With