Skip to content

Instantly share code, notes, and snippets.

@sugiana
Created October 21, 2013 03:38
Show Gist options
  • Save sugiana/7078312 to your computer and use it in GitHub Desktop.
Save sugiana/7078312 to your computer and use it in GitHub Desktop.
"""
Reference:
http://depa.usst.edu.cn/chenjq/www2/SDesign/JavaScript/CRCcalculation.htm
Input:
CRC Order: 32
CRC Polynom: 4C11DB7
Data Input:
Type: Hex
Value: 0403020108070605
Result:
CRC (hex): CA10A083
Install crcmod module first:
pip install crcmod
"""
import crcmod
import binascii
# Create function
func = crcmod.mkCrcFun(poly=0x104C11DB7, initCrc=0, rev=False, xorOut=0x0)
# Get input
raw = '\x04\x03\x02\x01\x08\x07\x06\x05'
print([raw])
print([binascii.hexlify(raw)])
# Calculate CRC
crc_result = func(raw)
print(hex(crc_result))
# Is match ?
crc_check = 0xca10a083
if crc_check == crc_result:
print('CRC match.')
else:
print('CRC mismatch.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment