How do I calculate CRC32 ?


CRC32 is a popular checksum algorithm used to detect data corruption. Multiple variants of the algorithm exist which have similar mathematical properties. The most common variant of the CRC32 checksum, sometimes called CRC-32b, is based on the following generator polynomial:

g(x) = x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1.

Method1:
Usage of the Crc32 class, to calculate crc32 in Int32 format:

uint finalValue = 0;
 
CheckSum.Crc32 crc32 = new CheckSum.Crc32();
 foreach (byte b in crc32.ComputeHash(data))
 {
  hash += b.ToString("x2").ToLower();
  }
  finalValue = Convert.ToUInt32(hash, 16);

-----------------------------------------------------------------------

Method2:
Usage of the Crc32 class, to calculate crc32 string:

Crc32 crc32 = new Crc32();
String hash = String.Empty;
using (FileStream fs = File.Open("c:\\myfile.txt", FileMode.Open))
  foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
Console.WriteLine("CRC-32 is {0}", hash);

Post a Comment

0 Comments