Click here to Skip to main content
6,595,444 members and growing! (14,632 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Encryption     Intermediate

Cyclic Redundancy Check (CRC32) HashAlgorithm

By Phil Bolduc

CRC32 HashAlgorithm
C#, Windows, .NET 1.0, Dev
Posted:3 Jun 2002
Updated:4 Oct 2002
Views:219,203
Bookmarked:66 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
26 votes for this article.
Popularity: 4.76 Rating: 3.37 out of 5
2 votes, 10.0%
1
1 vote, 5.0%
2
2 votes, 10.0%
3
7 votes, 35.0%
4
8 votes, 40.0%
5

Sample Image - CRC32_DotNet1.jpg

Sample Image - CRC32_DotNet1.jpg

Caution

As Paul Ravier mentioned, this class should NOT be used for security purposes. The CRC32 algorithm can easily be brute forced in minutes. For security applications use MD5, SHA1, SHA256, SHA384, or SHA512 in the System.Security.Cryptography namespace.

Introduction

The System.Security.Cryptography contains the HashAlgorithm. The HashAlgorithm class represents the base class from which all implementations of cryptographic hash algorithms must derive. Although I don't believe the Cyclic Redundancy Check algorithm is considered a true cryptographic hash algorithm, it does fit into the HashAlgorithm pattern.

The CRC32 algorithm is initialized using a 32-bit unsigned integer. This unsigned integer is known the polynomial. Different polynomials produce different check sums. The polynomial is used to generate a 256 element table that speeds the calculation. Normal applications will use the same polynomial throughout it's life-time. Due to this, the default behavior of the CRC32 class caches the table on first use. This behavior can be changed by setting the AutoCache property to false

Sample Usage

// Generic function using HashAlgorithm 

public void HashData(HashAlgorithm hashAlg, string str )
{
    byte[] rawBytes = System.Text.ASCIIEncoding.ASCII.GetBytes( str );
    byte[] hashData = hashAlg.ComputeHash( rawBytes );
    Console.WriteLine( BitConverter.ToString( hashData ) );
}

// Example showing that both functions can be used

public void Example()
{
    string str = "A lazy brown dog..."; // data to hash


    MD5 md5 = new MD5CryptoServiceProvider();
    HashData( md5, str); // Sample of the MD5 algorithm 


    CRC32 crc = new CRC32(); // equivalent to new CRC32(CRC32.DefaultPolynomial);

    HashData( crc, str); // Compute the default CRC32 value for a string

}

Conclusion

Although the MD5, SHA1, SHA256, SHA384, and SHA512 are more accurate than the CRC32 algorithm, sometimes you do not have a choice due various reasons. This class can be used without modification anywhere other HashAlgorithm classes can.

History

6 Oct 2002 - updated source

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Phil Bolduc


Member

Occupation: Web Developer
Location: Canada Canada

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 52 (Total in Forum: 52) (Refresh)FirstPrevNext
GeneralCRC32 to String PinmemberMPTP2:19 2 Aug '09  
GeneralWhat is the collision probabilty PinmemberepnetRob9:38 23 Sep '08  
QuestionEL Logging PinmemberOnSeCalme10:37 17 Sep '08  
GeneralOpen Source CRC32 Utility PinsussLiquid Wax11:08 1 Sep '04  
GeneralFaster, faster! Pinmembercookd20:29 16 Jul '04  
GeneralRe: Faster, faster! PinmemberHolgerK1:04 22 Jul '05  
GeneralRe: Faster, faster! PinmemberMadHatter ¢20:33 28 Mar '06  
GeneralModified + Tested version available PinmemberSteven Campbell11:50 28 Mar '04  
GeneralRe: Modified + Tested version available PinmemberPhil Bolduc21:54 28 Mar '04  
GeneralReflect ?? PinmemberAntonio Barros9:23 23 Dec '03  
GeneralThis CRC32 algorithm is broken Pinmembercbruun11:20 18 Dec '03  
GeneralRe: This CRC32 algorithm is broken Pinmembercbruun13:51 18 Dec '03  
GeneralRe: This CRC32 algorithm is broken PinmemberSteven Campbell10:35 28 Mar '04  
GeneralRe: This CRC32 algorithm is broken PinmemberPhil Bolduc21:56 28 Mar '04  
GeneralRe: This CRC32 algorithm is broken PinmemberVitaly Rudchenko1:54 7 May '07  
GeneralRe: This CRC32 algorithm is broken Pinmemberron wilson12:38 7 Sep '05  
GeneralRe: This CRC32 algorithm is broken PinmemberWillemM22:22 3 May '04  
GeneralRe: This CRC32 algorithm is broken Pinmembercbruun5:57 10 May '04  
GeneralRe: This CRC32 algorithm is broken PinmemberWillemM6:07 10 May '04  
Generalnew public byte[] ComputeHash ---> necessary? Pinmembercuynen8:46 20 Nov '03  
GeneralCRC calcs PinmemberTim Kohler5:00 21 Oct '03  
GeneralClarification of statement PinmemberTom Archer10:44 9 Sep '03  
GeneralRe: Clarification of statement PinmemberMika11:25 15 Dec '03  
GeneralHow I could hash a file? Pinmemberdomain_gr4:13 19 Jun '03  
GeneralCompatibility with other CRC32's Pinmembermmthomas17:06 7 Jan '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Oct 2002
Editor: Chris Maunder
Copyright 2002 by Phil Bolduc
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project