{"id":1432,"library":"crcmod","title":"CRCmod","description":"CRCmod is a Python module for calculating Cyclic Redundancy Checks (CRCs). It provides functionality to generate custom CRC algorithms and access a wide range of predefined CRC algorithms, such as CRC-CCITT and CRC-32. The current version is 1.7. The library has a slow release cadence, with updates primarily focused on bug fixes and stability.","status":"active","version":"1.7","language":"en","source_language":"en","source_url":"https://github.com/nicoretti/python-crcmod","tags":["crc","checksum","data-integrity","utility"],"install":[{"cmd":"pip install crcmod","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"crcmod","correct":"import crcmod"},{"note":"While `crcmod.Crc` exists, `crcmod.predefined.Crc` is commonly used for standard CRC algorithms.","wrong":"from crcmod import Crc","symbol":"Crc","correct":"from crcmod.predefined import Crc"},{"note":"`mkCrcFun` is a function that *returns* a CRC calculation function, it's not a method to be called directly on the module.","wrong":"crcmod.mkCrcFun()","symbol":"mkCrcFun","correct":"from crcmod import mkCrcFun"}],"quickstart":{"code":"import crcmod\nimport crcmod.predefined\n\n# 1. Using a predefined CRC algorithm (object-oriented approach)\n# Get a CRC-16-CCITT-FALSE calculator\ncrc16_ccitt = crcmod.predefined.Crc('crc-ccitt-false')\ndata_bytes = b'Hello, world!'\ncrc16_ccitt.update(data_bytes)\nprint(f\"CRC-16-CCITT-FALSE for '{data_bytes.decode()}': {hex(crc16_ccitt.crcValue)}\")\n\n# You can reset and reuse the object\ncrc16_ccitt.reset()\ncrc16_ccitt.update(b'Another test')\nprint(f\"CRC-16-CCITT-FALSE for 'Another test': {hex(crc16_ccitt.crcValue)}\")\n\n# 2. Creating a custom CRC function (functional approach)\n# Example: CRC-32 (Ethernet) - polynomial 0x104C11DB7, initial 0xFFFFFFFF, xorOut 0xFFFFFFFF, revIn/Out True\ncrc32_func = crcmod.mkCrcFun(0x104C11DB7, initCrc=0xFFFFFFFF, xorOut=0xFFFFFFFF, rev=True)\ndata_bytes_32 = b'123456789'\nresult_32 = crc32_func(data_bytes_32)\nprint(f\"CRC-32 for '{data_bytes_32.decode()}': {hex(result_32)}\")\n\n# 3. Using a predefined CRC algorithm (functional approach)\ncrc32_predef_func = crcmod.predefined.mkCrcFun('crc-32')\nresult_predef_32 = crc32_predef_func(b'123456789')\nprint(f\"CRC-32 (predefined func) for '123456789': {hex(result_predef_32)}\")","lang":"python","description":"This quickstart demonstrates how to calculate CRCs using both predefined algorithms and custom parameters. It shows both the object-oriented approach (`Crc` objects) and the functional approach (`mkCrcFun` functions) for flexibility."},"warnings":[{"fix":"Always encode strings to bytes before passing them to crcmod functions, e.g., `my_string.encode('utf-8')`.","message":"Input data to CRC functions (e.g., `update`, `mkCrcFun`) MUST be bytes (`bytes`), not strings (`str`). Passing a Python string will result in a `TypeError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `crcmod.predefined.Crc('crc-name')` for an object to update incrementally with a standard CRC. Use `crcmod.mkCrcFun(...)` or `crcmod.predefined.mkCrcFun('crc-name')` if you need a simple function that calculates the CRC of an entire byte string in one call.","message":"There are different ways to use crcmod: `crcmod.predefined.Crc` (object-oriented for standard CRCs), `crcmod.mkCrcFun` (functional for custom CRCs), and `crcmod.predefined.mkCrcFun` (functional for standard CRCs). Confusing these can lead to errors.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}