Skip to content

Instantly share code, notes, and snippets.

@evansneath
Created January 27, 2013 22:29
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save evansneath/4650991 to your computer and use it in GitHub Desktop.
Save evansneath/4650991 to your computer and use it in GitHub Desktop.
Performs a cyclic redundancy check implemented in Python 3.3 with examples. More about CRC can be found here: http://en.wikipedia.org/wiki/Cyclic_redundancy_check
#!/usr/bin/env python3
def crc(msg, div, code='000'):
"""Cyclic Redundancy Check
Generates an error detecting code based on an inputted message
and divisor in the form of a polynomial representation.
Arguments:
msg: The input message of which to generate the output code.
div: The divisor in polynomial form. For example, if the polynomial
of x^3 + x + 1 is given, this should be represented as '1011' in
the div argument.
code: This is an option argument where a previously generated code may
be passed in. This can be used to check validity. If the inputted
code produces an outputted code of all zeros, then the message has
no errors.
Returns:
An error-detecting code generated by the message and the given divisor.
"""
# Append the code to the message. If no code is given, default to '000'
msg = msg + code
# Convert msg and div into list form for easier handling
msg = list(msg)
div = list(div)
# Loop over every message bit (minus the appended code)
for i in range(len(msg)-len(code)):
# If that messsage bit is 1, perform modulo 2 multiplication
if msg[i] == '1':
for j in range(len(div)):
# Perform modulo 2 multiplication on each index of the divisor
msg[i+j] = str((int(msg[i+j])+int(div[j]))%2)
# Output the last error-checking code portion of the message generated
return ''.join(msg[-len(code):])
# TEST 1 ####################################################################
print('Test 1 ---------------------------')
# Use a divisor that simulates: x^3 + x + 1
div = '1011'
msg = '11010011101100'
print('Input message:', msg)
print('Divisor:', div)
# Enter the message and divisor to calculate the error-checking code
code = crc(msg, div)
print('Output code:', code)
# Perform a test to check that the code, when run back through, returns an
# output code of '000' proving that the function worked correctly
print('Success:', crc(msg, div, code) == '000')
# TEST 2 ####################################################################
print('Test 2 ---------------------------')
# Use a divisor that simulates: x^2 + 1
div = '0101'
msg = '00101111011101'
print('Input message:', msg)
print('Divisor:', div)
# Enter the message and divisor to calculate the error-checking code
code = crc(msg, div)
print('Output code:', code)
# Perform a test to check that the code, when run back through, returns an
# output code of '000' proving that the function worked correctly
print('Success:', crc(msg, div, code) == '000')
@aeroaks
Copy link

aeroaks commented Oct 15, 2015

It is 16 bit or 32 bit?

@Valdza83
Copy link

This calculates CRC-3 - 3 bits is appended to input message
Here is implemented this example:
https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Computation

@N3TBOY
Copy link

N3TBOY commented Oct 10, 2016

is it checking binarys ? what is the success msg?

@bolchisb
Copy link

hi,
can you modify it for X^15+1?
thats a '1000000000000001' poly

@veb-101
Copy link

veb-101 commented Oct 3, 2019

Thank you.

@GorgeousOne
Copy link

GorgeousOne commented Jan 24, 2021

Used this for a homework of mine. I thought perhaps the string lists could be replaced with integer lists:

def crc(msg, div, code='000'):
    msg_bits = [int(bit) for bit in msg]
    div_bits = [int(bit) for bit in div]

    for i in range(len(msg_bits) - len(div_bits)):
        if msg_bits[i] == 1:
            for j in range(len(div_bits)):
                msg_bits[i + j] = msg_bits[i + j] ^ div_bits[j]

    return ''.join(map(str, msg_bits[-len(code):]))

I don't think it really matters but it felt more convenient to me.
Anyway, thx for the solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment