crcmod.predefined
– CRC calculation using predefined algorithms¶
This module provides a function factory mkPredefinedCrcFun()
and a class PredefinedCrc
for calculating CRCs of byte strings using common predefined CRC algorithms.
The function factory and the class are very similar to those defined in crcmod
,
except that the CRC algorithm is specified by a predefined name, rather than the
individual polynomial, reflection, and initial and final-XOR parameters.
Predefined CRC algorithms¶
The crcmod.predefined
module offers the following predefined algorithms:
Name |
Polynomial |
Reversed? |
Init-value |
XOR-out |
Check |
---|---|---|---|---|---|
|
0x107 |
False |
0x00 |
0x00 |
0xF4 |
|
0x139 |
True |
0x00 |
0x00 |
0x15 |
|
0x11D |
False |
0xFD |
0x00 |
0x7E |
|
0x107 |
False |
0x55 |
0x55 |
0xA1 |
|
0x131 |
True |
0x00 |
0x00 |
0xA1 |
|
0x107 |
True |
0xFF |
0x00 |
0xD0 |
|
0x19B |
True |
0x00 |
0x00 |
0x25 |
|
0x18005 |
True |
0x0000 |
0x0000 |
0xBB3D |
|
0x18005 |
False |
0x0000 |
0x0000 |
0xFEE8 |
|
0x18005 |
False |
0x800D |
0x0000 |
0x9ECF |
|
0x10589 |
False |
0x0001 |
0x0001 |
0x007E |
|
0x13D65 |
True |
0xFFFF |
0xFFFF |
0xEA82 |
|
0x13D65 |
False |
0xFFFF |
0xFFFF |
0xC2B7 |
|
0x11021 |
False |
0x0000 |
0xFFFF |
0xD64E |
|
0x18005 |
True |
0xFFFF |
0xFFFF |
0x44C2 |
|
0x11021 |
True |
0xFFFF |
0x0000 |
0x6F91 |
|
0x11021 |
True |
0x554D |
0x0000 |
0x63D0 |
|
0x18BB7 |
False |
0x0000 |
0x0000 |
0xD0DB |
|
0x1A097 |
False |
0x0000 |
0x0000 |
0x0FB3 |
|
0x18005 |
True |
0x0000 |
0xFFFF |
0xB4C8 |
|
0x11021 |
True |
0x0000 |
0xFFFF |
0x906E |
|
0x11021 |
False |
0x0000 |
0x0000 |
0x31C3 |
|
0x18005 |
True |
0xFFFF |
0x0000 |
0x4B37 |
|
0x11021 |
True |
0x0000 |
0x0000 |
0x2189 |
|
0x11021 |
False |
0xFFFF |
0x0000 |
0x29B1 |
|
0x11021 |
False |
0x1D0F |
0x0000 |
0xE5CC |
|
0x1864CFB |
False |
0xB704CE |
0x000000 |
0x21CF02 |
|
0x15D6DCB |
False |
0xFEDCBA |
0x000000 |
0x7979BD |
|
0x15D6DCB |
False |
0xABCDEF |
0x000000 |
0x1F23B8 |
|
0x104C11DB7 |
True |
0x00000000 |
0xFFFFFFFF |
0xCBF43926 |
|
0x104C11DB7 |
False |
0x00000000 |
0xFFFFFFFF |
0xFC891918 |
|
0x11EDC6F41 |
True |
0x00000000 |
0xFFFFFFFF |
0xE3069283 |
|
0x1A833982B |
True |
0x00000000 |
0xFFFFFFFF |
0x87315576 |
|
0x104C11DB7 |
False |
0xFFFFFFFF |
0x00000000 |
0x0376E6E7 |
|
0x104C11DB7 |
False |
0xFFFFFFFF |
0xFFFFFFFF |
0x765E7680 |
|
0x1814141AB |
False |
0x00000000 |
0x00000000 |
0x3010BF7F |
|
0x104C11DB7 |
True |
0xFFFFFFFF |
0x00000000 |
0x340BC6D9 |
|
0x1000000AF |
False |
0x00000000 |
0x00000000 |
0xBD0BE338 |
|
0x1000000000000001B |
True |
0x0000000000000000 |
0x0000000000000000 |
0x46A5A9388A5BEFFE |
|
0x142F0E1EBA9EA3693 |
False |
0x0000000000000000 |
0xFFFFFFFFFFFFFFFF |
0x62EC59E3F1A4F00A |
|
0x1AD93D23594C935A9 |
True |
0xFFFFFFFFFFFFFFFF |
0x0000000000000000 |
0xCAA717168609F281 |
Notes
mkPredefinedCrcFun()
– CRC function factory¶
The function factory provides a simple interface for CRC calculation. It is similar
to crcmod.mkCrcFun()
, except that it specifies a CRC algorithm by name rather
than its parameters.
- crcmod.predefined.mkPredefinedCrcFun(crc_name)¶
Function factory that returns a new function for calculating CRCs using a specified CRC algorithm.
- Parameters:
crc_name (string) – The name of the predefined CRC algorithm to use.
- Returns:
CRC calculation function
- Return type:
function
The function that is returned is the same as that returned by
crcmod.mkCrcFun()
:- .crc_function(data[, crc=initCrc])¶
- Parameters:
data (byte string) – Data for which to calculate the CRC.
crc – Initial CRC value.
- Returns:
Calculated CRC value.
- Return type:
integer
- crcmod.predefined.mkCrcFun(crc_name)¶
This is an alias for
crcmod.predefined.mkPredefinedCrcFun()
. However, it is not defined whencrcmod.predefined
is imported using the form:>>> from crcmod.predefined import *
Examples¶
CRC-32 example:
>>> import crcmod.predefined
>>> crc32_func = crcmod.predefined.mkCrcFun('crc-32')
>>> hex(crc32_func('123456789'))
'0xcbf43926L'
XMODEM example:
>>> xmodem_crc_func = crcmod.predefined.mkCrcFun('xmodem')
>>> hex(xmodem_crc_func('123456789'))
'0x31c3'
Class PredefinedCrc
¶
This class is inherited from the crcmod.Crc
class, and is the same except for the
initialization. It specifies a CRC algorithm by name rather than its parameters.
- class crcmod.predefined.PredefinedCrc(crc_name)¶
Returns a new
Crc
object for calculating CRCs using a specified CRC algorithm.The parameter is the same as that for the factory function
crcmod.predefined.mkPredefinedCrcFun()
.- Parameters:
crc_name (string) – The name of the predefined CRC algorithm to use.
- class crcmod.predefined.Crc(poly[, initCrc, rev, xorOut])¶
This is an alias for
crcmod.predefined.PredefinedCrc
. However, it is not defined whencrcmod.predefined
is imported using the form:>>> from crcmod.predefined import *
Examples¶
CRC-32 Example:
>>> import crcmod.predefined
>>> crc32 = crcmod.predefined.Crc('crc-32')
>>> crc32.update('123456789')
>>> hex(crc32.crcValue)
'0xcbf43926L'
>>> crc32.hexdigest()
'CBF43926'