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
?
To show any integer's lowest 32 bits as 8 hexadecimal digits, without sign, you can "mask" the value by bit-and'ing it with a mask made of 32 bits all at value 1, then apply formatting. I.e.:
It's quite irrelevant whether the integer you are thus formatting comes from
zlib.crc32
or any other computation whatsoever.Merge the above 2 codes as below:
You can use base64 for getting out like [ERD45FTR]. And zlib.crc32 provides update options.
solution:
don't realy know for what is (excludeLine="", includeLine="")...
hashlib-compatible interface for CRC-32 support:
A little more compact and optimized code
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.