Click here to Skip to main content
Click here to Skip to main content

Cyclic Redundancy Check (CRC32) HashAlgorithm

By , 4 Oct 2002
 

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
Web Developer
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: This CRC32 algorithm is brokenmemberPhil Bolduc28 Mar '04 - 20:56 
I will have to find some time to update the source with the corrections listed here. Sorry for any inconvience fellow Code Project users.
 
Phil

GeneralRe: This CRC32 algorithm is brokenmemberVitaly Rudchenko7 May '07 - 0:54 
To fix the problem insert Initialize() into begin of ComputeHash function.
GeneralRe: This CRC32 algorithm is brokenmemberron wilson7 Sep '05 - 11:38 
this solution is the same as setting your default polynomial to the 'official' pkzip value, 0xEDB88320, i.e. Reflect(0xEDB88320) = 0x04C11DB7. also, using the default polynomial and its reflection gives different hash results.
GeneralRe: This CRC32 algorithm is brokenmemberWillemM3 May '04 - 21:22 
255 | 255 = True
191 | 244 = False
16 | 225 = False
192 | 218 = False
 
I got this result from a simple hashing.
I hashed the same text twice, and the result is not equal.
 
I am sorry, but your hash sure is broken.
 
Greetings.... Smile | :)


GeneralRe: This CRC32 algorithm is brokenmembercbruun10 May '04 - 4:57 
Willem,
 
What are you referring to ? The table I published or ... ?
 
Regards
Claus
GeneralRe: This CRC32 algorithm is brokenmemberWillemM10 May '04 - 5:07 
If I hash the text "Compad 5" two times, I get different results using your CRC32 CryptoService Provider.
 
Greetings.... Smile | :)


Questionnew public byte[] ComputeHash ---> necessary?membercuynen20 Nov '03 - 7:46 
redefining ComputeHash and all its overloads isn't necessary
 
HashAlgorithm contains implementations which internally use
+ Initialize
+ HashCore
+ HashFinal
 
nevertheless, you could still override ComputeHash, e.g. to optimize a Stream buffer
GeneralCRC calcsmemberTim Kohler21 Oct '03 - 4:00 
Hey, I've always used CRCs to do data validation checks on both ends of a communications channel.
You can CRC on the send end and send the value to the client along with the data. The client can then CRC and if the values match you can be very sure (althought not 100%) you got good data. If the client calc's a different CRC than the server's value you need to have the server resend.
 
I think CRC is used for this purpose, not cryptography.
 

GeneralClarification of statementmemberTom Archer9 Sep '03 - 9:44 
You make the following point in your 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.
 
What are examples of these situations?
 
Cheers,
Tom Archer
Inside C#,
Extending MFC Applications with the .NET Framework
It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson
GeneralRe: Clarification of statementmemberMika15 Dec '03 - 10:25 
Well I can think of one off the top of my head - Integrating some code with current code - for example when you ZIP some files, the ZIP stores the CRC's of the compressed files to check whether they have been corrupted after un-zipping.
 
Maybe not used for security but definitely has a place.

 
Mladen Mihajlovic
http://mladen.nata.co.za

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 5 Oct 2002
Article Copyright 2002 by Phil Bolduc
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid