§ May 3, 2005

A CRC32 implementation in C#

The other day I was looking for a simple CRC32 library...

I couldn't find one (that suited my needs) so I wrote a quick and dirty implementation.

here it is:

/******************************************************/
/*          NULLFX FREE SOFTWARE LICENSE              */
/******************************************************/
/*  CRC32 Library                                     */
/*  by: Steve Whitley                                 */
/*  © 2005 NullFX Software                            */
/*                                                    */
/* NULLFX SOFTWARE DISCLAIMS ALL WARRANTIES,          */
/* RESPONSIBILITIES, AND LIABILITIES ASSOCIATED WITH  */
/* USE OF THIS CODE IN ANY WAY, SHAPE, OR FORM        */
/* REGARDLESS HOW IMPLICIT, EXPLICIT, OR OBSCURE IT   */
/* IS. IF THERE IS ANYTHING QUESTIONABLE WITH REGARDS */
/* TO THIS SOFTWARE BREAKING AND YOU GAIN A LOSS OF   */
/* ANY NATURE, WE ARE NOT THE RESPONSIBLE PARTY. USE  */
/* OF THIS SOFTWARE CREATES ACCEPTANCE OF THESE TERMS */
/*                                                    */
/* USE OF THIS CODE MUST RETAIN ALL COPYRIGHT NOTICES */
/* AND LICENSES (MEANING THIS TEXT).                  */
/*                                                    */
/******************************************************/


namespace NullFX.Security {
    using System;
    public class Crc32 {
        uint[] table;

        public uint ComputeChecksum(byte[] bytes) {
            uint crc = 0xffffffff;
            for(int i = 0; i < bytes.Length; i++) {
                byte index = (byte)(((crc) & 0xff) ^ bytes[i]);
                crc = (uint)((crc >> 8) ^ table[index]);
            }
            return ~crc;
        }

        public Crc32() {
            uint poly = 0xedb88320;
            table = new uint[256];
            uint temp = 0;
            for(int i = 0; i < table.Length; i++) {
                temp = i;
                for(int j = 8; j > 0; j--) {
                    if((temp & 1) == 1) {
                        temp = (uint)((temp >> 1) ^ poly);
                    }else {
                        temp >>= 1;
                    }
                }
                table[i] = temp;
            }
        }
    }
}

Its admittedly small, and hardly anyone uses CRC32 for security purposes (at least I hope not), but it is a decent checksum generator for small non-critical items.

I needed a 32 bit sized number generator--I was implementing my own object.GetHashCode() and wanted to get an int hashcode for my own objects--and this seemed like a decent way to do it.

If you need for this code to be CLS compliant, you can change the method signature's return type from uint to long and it will operate the same (the uint crc value will be implicitly converted from uint to long)

Links to the other C# CRC implementations CRC16-CCITT, CRC16
Posted 3 years, 10 months ago on May 3, 2005

 Comments can be posted in the forums.

© 2003 - 2008 NullFX