Reputation: 9166
I am trying to CRC16/32
checksum with binascii.crc32
, binacsii.crc_hqx
This is the values I used as an example(I got this example values from some network protocol specification I am reading).
CRC 16
input(as ASCII string):123456789
seed:0x0000
hash result:0x31c3
CRC 32
input(as ASCII string):123456789
seed:0x00000000
hash result:0xd202d277
Below is my test code with above test values.
>>> import binascii
>>> hex(binascii.crc_hqx("123456789", 0x0000))
'0x31c3' #corret result, same as test value result
>>> hex(binascii.crc32("123456789", 0x00000000) & 0xFFFFFFFF)
'0xcbf43926L'#Wrong result, different value from test value result, It Must be 0xd202d277
What is a problem...?
Actually I found this sentence in Python doc Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm.
Does it mean I can't use it for CRC32 checksum?
If so, Is there any recommendation?
----------------------Edit----------------------
I didn't make the test values myself.
I Just got the values from a document explaining CRC32.
Below is the table I referenced
----------------------Edit----------------------
In the document containing the figure table, It describes G2S_crc like below.
The CRC-32 polynomial defined for G2S can be described as follows:
x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
It is also represented as 0x04C11DB7. This polynomial is also used by IEEE 802.3.
.
function crc(bit array bitString[1..len], int polynomial)
{
shiftRegister := initial value // either the seed or all 1 bits
for i from 1 to len
{
if most significant bit of shiftRegister = 1
{
// Shift left, place data bit as LSB, then divide
shiftRegister := shiftRegister left shift 1
shiftRegister := shiftRegister or bitString[i]
shiftRegister := shiftRegister xor polynomial
}
else
{
// shiftRegister is not divisible by polynomial yet.
// Just shift left and bring current data bit onto LSB of shiftRegister
shiftRegister := shiftRegister left shift 1
shiftRegister := shiftRegister or bitString[i]
}
}
return shiftRegister
}
Is it different from binascii
?
Upvotes: 1
Views: 6834
Reputation: 1076
From this CRC catalog, you seem to be using CRC-32/MPEG-2. Whereas python uses plain CRC32, with polynom 0xEDB88320
.
BTW, the CRC returned by python is an int
. And python's int are signed. If you need the unsigned representation, you need to invert the result, like hex(~0x76e943d9 & 0xffffffff)
.
Upvotes: 2
Reputation: 304463
This online calculator
http://www.lammertbies.nl/comm/info/crc-calculation.html
agrees with Python
Can you quote the source you got 0xd202d277
from? There are variations regarding bit order etc.
Upvotes: 4