Groups | Blog | Home
all groups > dotnet framework > may 2006 >

dotnet framework : How to compute CRC


Bob Altman
5/7/2006 2:11:09 PM
I am processing a very large (20+ GB) file. As part of my processing I need
to compute a CRC or hash value for the file. To improve the efficiency of
my algorithm I'm looking for a way to do this without having to read the
file twice (once for my processing and again to compute the hash).

My processing involves reading the file one 64K byte chunk at a time into a
byte array. As I'm reading the file I'd like to generate a running CRC or
hash code that I can update each time I read another bunch of bytes from the
file. Is there a way to do this without writing my own CRC32
implementation?

Thanks!

- Bob

Carl Daniel [VC++ MVP]
5/7/2006 2:13:01 PM
[quoted text, click to view]

Yes.

While the .NET framework doesn't include a CRC32 implementation, there are
many articles/libraries available that do provide just one you're looking
for.

Here's one:

http://www.codeproject.com/csharp/crc32_dotnet.asp?df=100&forumid=4026&exp=0&select=913712

that exposes CRC32 as a class derived from
System.Security.Cryptography.HashAlgorithm. You can easily hash an
artibrarily large object in blocks as you read it in and process it.

If you don't like that one, do a few network searches - there are literally
dozens of implementations out there.

-cd

Bob Altman
5/7/2006 2:54:17 PM
Thanks for the instant answer! I guess my question should have been phrased
a little more generally: Is there a way to use any of the existing hash
algorithms to compute a hash for an arbitrarily large file in blocks as I
read and process it?

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
[quoted text, click to view]

Carl Daniel [VC++ MVP]
5/7/2006 5:00:36 PM
[quoted text, click to view]

Yes. Take the CRC implementation that's a HashAlgorithm and use it as the
"Transform" for a System.Security.CryptoStream. If you don't care that it's
CRC32 in particular, the .NET framekwork already provides SHA1 and MD5 (and
other) hashes as HashTransforms.

-cd

Göran_Andersson
5/8/2006 12:00:40 AM
You could implement it as a stream reader, e.g. you make a class that
you use to read the stream, and the class calculates the hash as it
reads from the stream.

[quoted text, click to view]
AddThis Social Bookmark Button