I need to use a crc checksum in a python3 program, but my knowledge of crc is virtually non-existent.
Here is the test code I wrote
import crcmod
crc_func = crcmod.mkCrcFun(0x1d, initCrc=0x07, xorOut=0x00)
print(hex(crc_func(b'123456789')))
When I run this, I get the following error:
ValueError: The degree of the polynomial must be 8, 16, 24, 32 or 64
But 1D is 8 bit, so I must be doing something wrong. Please explain what I did wrong.
No it isn't; it's 5 bits:
The way this module defines things (see the
_verifyPoly
function) rounds down, so this counts as a "4-bit polynomial". An "8-bit polynomial" has to be between0x100
and0x1ff
(inclusive). Of course most of the polynomials in that range will not give useful results, but they can at least be handled by this module.As the docs say:
0x1d
does not generate an 8-degree polynomial.If none of this makes any sense to you, well, the docs explicitly say, right at the top:
If you don't want to learn how CRC works and figure out how to design and encode an appropriate polynomial yourself, just use one of the
predefined
ones.More generally, if you don't have a specific CRC polynomial in mind, or don't even understand what this means, you probably have no reason to use this module in the first place. If you just "need to use a crc checksum", there's already a perfectly good CRC function in the stdlib,
zlib.crc32
.For that matter, if it doesn't have to actually be a CRC, just a reasonably reliable checksum, you probably want
zlib.adler32
instead.Or, if
adler32
andcrc32
aren't sufficient for some reason, whatever that reason is, I'd wager you don't actually need a checksum but a real hash, and a better CRC polynomial isn't gong to help you; you want a different algorithm, probably something out ofhashlib
.