ScienceComputer Science

CRC
calculator.

Get the CRC-8, CRC-16 or CRC-32 checksum of a message, with the bytes to send.

How it's worked out

A cyclic redundancy check treats your message as one long binary number. It divides that number by a fixed binary number, the generator polynomial, and keeps the remainder. The remainder is the CRC. The division uses mod-2 arithmetic, where adding and subtracting are both XOR, so there are no carries or borrows.

Ross Williams' guide works one through by hand. First append as many zeros as the CRC has bits. Then, under every leading 1, XOR in the generator. Here the message is 1101011011 and the generator is 10011, a 4-bit CRC, so four zeros go on the end and the remainder is 1110.

Dividing 11010110110000 by 10011
BitUnder the generatorAfter XOR
11101001001
21001100000
71011000101
91010000111
CRC\displaystyle \text{CRC}=remainder of M(x)⋅xWG(x)\displaystyle {}=\;\text{remainder of } \frac{M(x) \cdot x^{W}}{G(x)}

Here M(x)M(x) is the message, WW is the CRC width in bits and G(x)G(x) is the generator. Real algorithms add four settings on top. The register starts at an initial value instead of 0. Each byte may be read low bit first, which is called reflected input. The result may be reflected too. Last, the result is XORed with a final value.

The CRC RevEng catalog names each set of settings as a model and gives its check value, the CRC of the text 123456789. That is why CRC-16/ARC and CRC-16/MODBUS share the polynomial 0x8005 yet give 0xBB3D and 0x4B37: only the initial value differs. The tool reads every bit with the same loop the catalog describes, and the working shows the register after each byte.

Worked examples

Priya builds a Modbus request by hand

Priya wants to read 10 registers from device 1 over a serial line. The request is the six bytes 01 03 00 00 00 0A: the address, the function code, the start register and the count.

CRC-16/MODBUS of those bytes is 0xCDC5. The Modbus serial line specification says the low byte goes first, so the frame on the wire ends with C5 CD.

A receiver can check the whole frame in one pass. Run all eight bytes through the same CRC and the register comes out at 0x0000, the residue the catalog lists for this model. Anything else means a byte was damaged or the CRC bytes were swapped.

Open this example in the tool

Tom tests his own CRC-32 code

Tom wrote a CRC-32 routine for a file format and wants to know if it is right. He feeds it the nine bytes of the text 123456789. The catalog check value is 0xCBF43926, and the tool gives 0xCBF43926.

His routine gives a different answer, so he compares the CRC after each byte. After the first byte, the character 1, it should be 0x83DCEFB7. After two bytes it should be 0x4F5344CD. The first byte where his value differs tells him where the bug starts.

Once the values agree, he appends the CRC, low byte first, and runs the codeword through. The register should read 0xDEBB20E3, which is the CRC-32 residue in the catalog, before the final XOR.

Open this example in the tool

Ana works out which CRC-16 a sensor uses

Ana's sensor manual says only "CRC-16". She sends it 123456789 and it answers 0xBB3D. On those bytes, CRC-16/MODBUS gives 0x4B37, CRC-16/XMODEM gives 0x31C3 and CRC-16/CCITT-FALSE gives 0x29B1.

She types BB3D as the CRC she expects, and the search finds one match: CRC-16/ARC. That model starts at 0 and reads bits low first. Now her code can use the same settings.

Open this example in the tool

Reading your result

The value. The answer is shown in hex, the way datasheets print it, with the decimal value under it. A 16-bit CRC is two bytes and a 32-bit CRC is four. The bytes to send come next. Reflected models usually send the low byte first, and the others send the high byte first.

The codeword check. Append the CRC to the data in that order and work out the register again. If the data arrived intact, you get a fixed number called the residue. For CRC-32 it is 0xDEBB20E3, and for the CRC-16 models here it is 0. The working ends with this check.

What it catches. Williams notes that a generator with at least two 1 bits catches every single flipped bit. For longer bursts of damage he cites Tanenbaum: the chance one slips through is about 1 in 2 to the power of W. That is about 1 in 4,294,967,296 for CRC-32.

What it does not do. A CRC is not secret. Anyone who changes the data can work out a new CRC that matches, so it does not prove the data came from you. For that, use a hash with a key, such as HMAC-SHA-256.

Common mistakes

  • A line ending sneaks in. CRC-32 of "Hello, world" is 0xE79AA9C2. With a newline on the end it is 0x475A3FA6, and with a Windows line ending it is 0x4264CCB6. Files and terminals often add one.
  • Text where you meant hex. Typed as hex, 31 32 33 gives 0x884863D2, the same as the text 123. Typed as text, the same eight characters are eight bytes, spaces included, and give 0xA88D909D.
  • The CRC bytes swapped. A Modbus CRC of 0xCDC5 goes on the wire as C5 CD. Sending the high byte first gives a frame that fails the check.
  • Trusting the name alone. "CRC-CCITT" means different things in different code. The catalog says the model often given that name is really CRC-16/IBM-3740, also called CCITT-FALSE, with check value 0x29B1. CRC-32C gives 0xE3069283 on the check string, not 0xCBF43926.

Questions people ask

Most of the time the bytes differ, not the maths: a trailing newline, a different text encoding, or hex typed as text. If the bytes match, the settings differ. Paste the value you expect into the search to see which model gives it.

Sources