Questions tagged [crc]

A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data.

A cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data. Blocks of data entering these systems get a short check value attached, based on the remainder of a polynomial division of their contents; on retrieval the calculation is repeated, and corrective action can be taken against presumed data corruption if the check values do not match. wikipedia

The most commonly used polynomial lengths are:

  • CRC-8: 9 bits
  • CRC-16: 17 bits
  • CRC-32: 33 bits
  • CRC-64: 65 bits

A truly excellent tutorial on CRC's is Ross Williams' “Painless Guide to CRC Detection Algorithms”, which can also be found here, here, here, here, and here.

Before you ask a question about CRC, have you first checked:

  • Which CRC algorithm is being discussed?
    • Be specific about the source of your CRC. If you're asking about the Ethernet CRC-32, say so. If you're asking about the Castagnoli CRC-32C, say so.
    • There exists a catalogue of CRC parameters and a list of CRC polynomials on Wikipedia, both of which can be used to name more exotic specimens.
    • Make sure to select the correct tag.
  • Polynomial: Is the polynomial correct?
    • Is it bit-reversed?
    • Is it byte-swapped?
    • Is it complemented?
  • Initialization: Is the CRC correctly initialized?
    • Some are initialized with all-zeros, some with all-ones, and some with something entirely different. Be sure to have the correct initialization.
    • For non-zero initialization it makes a difference whether the message is passed through the shift register or just XORed to its output.
  • Processing:
    • Is the data being CRCed correctly byte-swapped?
    • Are you shifting in the right direction?
    • Are you using any CRC intrinsics, like Intel's SSE4.2 crc32 instruction?
    • Is your lookup table correct for the polynomial and byte-order you chose?
  • Finalization: Is the CRC correctly finalized?
    • Some CRC schemes call for the polynomial result to be XORed with a particular constant, like 0xFFFFFFFF, while some don't.
    • The CRC output might be bit-reversed in your implementation.
977 questions
135
votes
13 answers

When is CRC more appropriate to use than MD5/SHA1?

When is it appropriate to use CRC for error detection versus more modern hashing functions such as MD5 or SHA1? Is the former easier to implement on embedded hardware?
Gili
  • 76,473
  • 85
  • 341
  • 624
67
votes
12 answers

What is the fastest hash algorithm to check if two files are equal?

What is the fastest way to create a hash function which will be used to check if two files are equal? Security is not very important. Edit: I am sending a file over a network connection, and will be sure that the file on both sides are equal
eflles
  • 5,890
  • 10
  • 38
  • 54
57
votes
2 answers

What checksum algorithm should I use?

I'm building a system which needs to be able to find if blobs of bytes have been updated. Rather than storing the whole blob (they can be up to 5MBs), I'm thinking I should compute a checksum of it, store this and compute the same checksum a little…
Julien Genestoux
  • 26,486
  • 19
  • 63
  • 91
55
votes
7 answers

Function to Calculate a CRC16 Checksum

I'm working on a library to provide simple reliable communication over an RS232 or RS485 connection. Part of this code involves using a CRC16 checksum on the data to detect corruption from line noise. I've created a function to calculate a CRC16…
Jonathan Lamothe
  • 663
  • 1
  • 5
  • 5
46
votes
2 answers

Probability of collision when using a 32 bit hash

I have a 10 character string key field in a database. I've used CRC32 to hash this field but I'm worry about duplicates. Could somebody show me the probability of collision in this situation? p.s. my string field is unique in the database. If the…
nguyenngoc101
  • 1,051
  • 2
  • 15
  • 26
40
votes
5 answers

How to use boost::crc?

I want to use boost::crc so that it works exactly like PHP's crc32() function. I tried reading the horrible documentation and many headaches later I haven't made any progress. Apparently I have to do something like: int GetCrc32(const string&…
Thomas Bonini
  • 40,716
  • 28
  • 117
  • 153
40
votes
6 answers

Data Length vs CRC Length

I've seen 8-bit, 16-bit, and 32-bit CRCs. At what point do I need to jump to a wider CRC? My gut reaction is that it is based on the data length: 1-100 bytes: 8-bit CRC 101 - 1000 bytes: 16-bit CRC 1001 - ??? bytes: 32-bit CRC EDIT: Looking at the…
Robert
  • 11,820
  • 19
  • 61
  • 85
30
votes
1 answer

How to calculate 32 bit CRC in Ruby on rails?

i want to calculate 32 bit CRC value for 'input field value" in Ruby on rails. need the sample code , please help me anyone.
Jeyavel
  • 2,774
  • 9
  • 32
  • 45
28
votes
10 answers

compute crc of file in python

I want to calculate the CRC of file and get output like: E45A12AC. Here's my code: #!/usr/bin/env python import os, sys import zlib def crc(fileName): fd = open(fileName,"rb") content = fd.readlines() fd.close() for eachLine in…
user203547
  • 303
  • 1
  • 3
  • 4
25
votes
2 answers

Trying to reverse engineer a packet checksum/CRC/hash

I have an old, no longer manufactured electronic device with a serial port. I am trying to reverse engineer the data packet CRC/checksum/hash used in this device. Anyone with keen eyes, sharp math skills out there who might be able to crack this…
MSunstrum
  • 251
  • 3
  • 4
22
votes
4 answers

definitive CRC for C

Since CRC is so widely used, I'm surprised by having a hard time finding CRC implementations in C. Is there a "definitive" CRC calculation snippet/algorithm for C, that "everyone" uses? Or: is there a good CRC implementation somebody can vouch for,…
oyvind
  • 1,270
  • 2
  • 11
  • 23
19
votes
3 answers

Can CRC32(C) ever return to 0?

I'm wondering if CRC32 sum and CRC32C in particular ever return to 0? The simple answer would be "yes" given a large enough data set. However, I was wondering if there is any provisioning in CRC32C standard that would explicitly prevent this from…
dtoux
  • 1,374
  • 2
  • 16
  • 34
18
votes
1 answer

What is cyclic redundancy check and how it works in simple terms (for-dummies style)?

I'm having trouble understanding the concept and workings of the ugly sounding term "cyclic redundancy check". I'm attending a college course on Computer Networks and I'm getting lost already. The trouble is that my understanding of mathematics is…
Boyko Arsov
  • 341
  • 1
  • 4
  • 12
18
votes
2 answers

How to generate a CRC-16 from C#

I am trying to generate a CRC-16 using C#. The hardware I am using for RS232 requires the input string to be HEX. The screenshot below shows the correct conversion, For a test, I need 8000 to be 0xC061, however the C# method that generates CRC-16…
dynamicuser
  • 1,422
  • 3
  • 23
  • 49
17
votes
2 answers

Get CRC checksum of an NSData in Objective-C

How can I count CRC (32 or 64) of an NSData object in Objective-C? Thanks in advance!
Knodel
  • 4,309
  • 7
  • 40
  • 64
1
2 3
65 66