CRC Algorithm implementation in C#

By | March 29, 2017

Introduction

My job requires me to interface with a lot of embedded systems. Often times when communicating with those embedded systems a CRC check value will need to be calculated. A lot of the code I write is in C# and although there a few solutions out there for computing CRCs in .NET/Mono, the majority are not complete or generic. They may work for a CRC32 calculation, but then break when you need to use them for a CRC32 which requires reflection in the lookup table and check value calculation. The goal for this project was to produce a CRC algorithm implementation in C# which could handle any CRC from 8 to 64 bits using the most commonly recognized CRC parameterization model.

Kudos

This project would not have been possible if it were not for…

Ross Williams’ A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS. Ross has graciously published this guide into the public domain. The guide goes through the theory and implementation of CRC algorithms and perhaps most famously defines a parameterization model (Rocksoft^tm Model) to categorize the many CRC variants. The parameterization model and C implementation of the CRC algorithm were a massive help in understanding how CRCs work and in implementing the CRC algorithm in C#.

The CRC RevEng project’s ‘Catalogue of parametrised CRC algorithms’ maintained by Greg Cook. The values in the catalog were used in the unit tests which verified the correctness of the implementation.

Code

Below is the C# class representing the aforementioned CRC Parameterization Model. It is composed of a constructor which validates and sets the properties which themselves are read-only.

The key characteristics of any CRC algorithm are:

  • Width: The width of the CRC algorithm in bits.
  • Polynomial: The polynomial of the CRC algorithm.
  • Initial Value: The initial value used in the computation of the CRC check value.
  • XOR Out Value: The value which is XORed to the final computed value before returning the check value.
  • Reflect In: Indicates whether bytes are reflected before being processed.
  • Reflect Out: Indicates whether the final computed value is reflected before the XOR stage.

The following CRC class generates a lookup table at runtime using the CRC Parameters passed into the constructor. The CRC check values for data can then be computed and returned to the caller as numeric values (i.e. 0xcbf43926) or as a byte array (i.e. { 0x26, 0x39, 0xf4, 0xcb }) depending on the method used.

Testing

At the time of this post, there are 83 CRC algorithm variants between 8 and 64 bits in width documented in CRC RevEng’s Catalog. All 83 variants have been unit tested using NUnit and successfully pass.

Below is a sample snippet from the CRC-32 Tests:

Concluding Remarks

The focus of this project was to create a lightweight CRC implementation for .NET/Mono software projects which could be easily imported into the source code of a software project or compiled and used as a DLL. I plan on hosting the project for download here on my website and soon I will put the project up on Github and as a NuGet package.

The project covers CRC 8-64 bits. CRC support for width under 8 bits is provided by a wonderful open source .NET/Mono project called Data.HashFunction which covers those CRCs as well as many more non-cryptographic hash functions.

Link to download project: here

Leave a Reply

Your email address will not be published. Required fields are marked *