How do I calculate CRC32 of a string

How do I calculate the CRC32 (Cyclic Redundancy Checksum) of a string in .NET?

Add Comment
6 Answer(s)

I also have a crc32 algorithm here, in VB.NET

http://blog.codinghorror.com/squishysyntaxhighlighter-and-crc32/

Answered on July 2, 2016.
Add Comment

This solution is one that I wound up going with and it seems to work very well:

http://www.techques.com/question/1-5059268/C
http://tomkaminski.com/crc32-hashalgorithm-c-net

The other solution mentioned here did not seem to produce the correct values. The code I’m building this for is for producing hashes of email addresses and phone numbers and the results of the code from the other solution was not matching the documentation. After deciding to use the solution at the URL above the values matched.

Thanks to the original author as well as to Tom Kaminski for refining the original code and published it at the URL above.

Looks like the link from Tom Kaminski now re-directs several times and lands on an ad. I’ve updated to another link with similar information.

Answered on July 2, 2016.
Add Comment
Answered on July 2, 2016.
Add Comment

This guy seems to have your answer.

http://damieng.com/blog/2006/08/08/Calculating_CRC32_in_C_and_NET

And in case the blog ever goes away or breaks the url, here’s the github link:

https://github.com/damieng/DamienGKit/blob/master/CSharp/DamienG.Library/Security/Cryptography/Crc32.cs

Answered on July 2, 2016.
Add Comment

C# CRC32 Framework 2

Program.cs

class Program
{
    static void Main(string[] args)
    {
        Thread threadCRC32 = new Thread(CRC32);
        threadCRC32.Start();

        bool exit = false;
        while (!exit)
        {
            var vExit = Console.ReadLine() == "exit" ? exit = true : exit = false;
        }
    }
    static void CRC32()
    {
        Crc32 crc32 = new Crc32();
        String hash = String.Empty;
        using (FileStream fs = File.OpenRead(Application.ExecutablePath))
        {
            foreach (byte b in crc32.ComputeHash(fs))
            {
                hash += b.ToString("x2").ToLower();
            }
            Console.WriteLine("this Application CRC-32 is {0}", hash);
        }
    }
}

CRC32.cs

using System;
using System.Security.Cryptography;
using System.IO;

public class Crc32 : HashAlgorithm
{
    public const UInt32 DefaultPolynomial = 0xedb88320;
    public const UInt32 DefaultSeed = 0xffffffff;

    private UInt32 hash;
    private UInt32 seed;
    private UInt32[] table;
    private static UInt32[] defaultTable;

    public Crc32()
    {
        table = InitializeTable(DefaultPolynomial);
        seed = DefaultSeed;
        Initialize();
    }

    public Crc32(UInt32 polynomial, UInt32 seed)
    {
        table = InitializeTable(polynomial);
        this.seed = seed;
        Initialize();
    }

    public override void Initialize()
    {
        hash = seed;
    }

    protected override void HashCore(byte[] buffer, int start, int length)
    {
        hash = CalculateHash(table, hash, buffer, start, length);
    }

    protected override byte[] HashFinal()
    {
        byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
        this.HashValue = hashBuffer;
        return hashBuffer;
    }

    public override int HashSize
    {
        get { return 32; }
    }

    public static UInt32 Compute(byte[] buffer)
    {
        return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length);
    }

    public static UInt32 Compute(UInt32 seed, byte[] buffer)
    {
        return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length);
    }

    public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
    {
        return ~CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length);
    }

    private static UInt32[] InitializeTable(UInt32 polynomial)
    {
        if (polynomial == DefaultPolynomial && defaultTable != null)
            return defaultTable;

        UInt32[] createTable = new UInt32[256];
        for (int i = 0; i < 256; i++)
        {
            UInt32 entry = (UInt32)i;
            for (int j = 0; j < 8; j++)
                if ((entry & 1) == 1)
                    entry = (entry >> 1) ^ polynomial;
                else
                    entry = entry >> 1;
            createTable[i] = entry;
        }

        if (polynomial == DefaultPolynomial)
            defaultTable = createTable;

        return createTable;
    }

    private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
    {
        UInt32 crc = seed;
        for (int i = start; i < size; i++)
            unchecked
            {
                crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff];
            }
        return crc;
    }

    private byte[] UInt32ToBigEndianBytes(UInt32 x)
    {
        return new byte[] {
            (byte)((x >> 24) & 0xff),
            (byte)((x >> 16) & 0xff),
            (byte)((x >> 8) & 0xff),
            (byte)(x & 0xff)
        };
    }

    public string Get(string FilePath)
    {
        Crc32 crc32 = new Crc32();
        String hash = String.Empty;

        using (FileStream fs = File.Open(FilePath, FileMode.Open))
            foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();

        return hash;
    }
}
Answered on July 2, 2016.
Add Comment

There’s a CrcCalculatorStream included in DotNetZip. The source is available here.

Answered on July 2, 2016.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.