CRC32 checksum in python with hex input

I want to calculate the CRC32 checksum of a string of hexadecimal values ​​in python. I found zlib.crc32 (data) and binascii.crc32 (data), but all the examples I found using these functions have β€œdata” as a string (for example, β€œhello”). I want to pass the hexadecimal values ​​as data and find the checksum. I tried to set the data as a hexadecimal value (e.g. 0x18329a7e), and I get TypeError: should be a string or buffer, not an int. The function evaluates when I make a hexadecimal value a string (for example, "0x18329a7e"), but I don't think it evaluates the correct checksum. Any help would be greatly appreciated. Thanks!

+4
source share
3 answers

I think you are looking for binascii.a2b_hex() :

 >>> binascii.crc32(binascii.a2b_hex('18329a7e')) -1357533383 
+12
source
 >>> import struct,binascii >>> ncrc = lambda numVal: binascii.crc32(struct.pack('!I', numVal)) >>> ncrc(0x18329a7e) -1357533383 
+1
source

Try converting a list of hexadecimal values ​​to a string:

 t = ['\x18', '\x32', '\x9a', '\x7e'] chksum = binascii.crc32(str(t)) 
0
source

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


All Articles