# How to Calculate CRC32 Checksums

Written by l.p. klages
• Share
• Tweet
• Share
• Pin
• Email

Calculating a checksum for a file began in the early days of computing. It helps answer the basic question: is this file the one I think it is? Is this file corrupt, or is it exactly the same as it was before? The basic logic is that by adding up all the ones in the binary representation of the file, you can get a single number that you can store for later. When you want to make sure the file is the same, you calculate the checksum again. If the new and old checksums are the same, the file is the same, if not, it’s different or corrupt. There are many algorithms to calculate checksums, one of which is the CRC32 algorithm.

Skill level:
Easy

## Instructions

1. 1

Copy and paste the following code into a C# class file: using System GO using System.Security.Cryptography GO

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

``````private UInt32 hash
``````

GO private UInt32 seed GO private UInt32[] table GO private static UInt32[] defaultTable GO

``````public Crc32() {
table = InitializeTable(DefaultPolynomial)
``````

GO seed = DefaultSeed GO Initialize() GO }

``````public Crc32(UInt32 polynomial, UInt32 seed) {
table = InitializeTable(polynomial)
``````

GO this.seed = seed GO Initialize() GO }

``````public override void Initialize() {
hash = seed
``````

GO }

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

GO }

``````protected override byte[] HashFinal() {
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash)
``````

GO this.HashValue = hashBuffer GO return hashBuffer GO }

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

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

GO }

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

GO }

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

GO }

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

GO

``````    UInt32[] createTable = new UInt32[256]
``````

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

``````    if (polynomial == DefaultPolynomial)
defaultTable = createTable
``````

GO

``````    return createTable
``````

GO }

``````private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size) {
UInt32 crc = seed
``````

GO for (int i = start; i < size; i++) unchecked { crc = (crc >> 8) ^ table[buffer[i] ^ crc & 0xff] GO } return crc GO }

``````private byte[] UInt32ToBigEndianBytes(UInt32 x) {
return new byte[] {
``````

(byte)((x >> 24) & 0xff), (byte)((x >> 16) & 0xff), (byte)((x >> 8) & 0xff), (byte)(x & 0xff) } GO } }

2. 2

Copy and paste the following code into the main loop of your program: Crc32 crc32 = new Crc32() GO String hash = String.Empty GO

using (FileStream fs = File.Open(\"c:\\myfile.txt\", FileMode.Open)) foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString(\"x2\").ToLower() GO

Console.WriteLine(\"CRC-32 is {0}\", hash);

3. 3

Change the value “c:\\myfile.txt” above to the file for which you want to calculate the checksum.

4. 4

Run your program to see the computed CRC32 checksum of the file you specified.

#### Tips and warnings

• You can calculate the checksum of a simple string by simply passing the string to the ComputeHash method instead of an entire FileStream.

### Don't Miss

#### References

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent