views:

1211

answers:

1

I am using following code to compute MD5SUM of a file -

byte[] b = System.IO.File.ReadAllBytes(file);
string sum = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(b));

This works fine normally, but if I encounter a large file (~1GB) - e.g. an iso image or a DVD VOB file - I get an Out of Memory exception.

Though, I am able to compute the MD5SUM in cygwin for the same file in about 10secs.

Please suggest how can I get this to work for big files in my program.

Thanks

+11  A: 

I suggest using the alternate method:

MD5CryptoServiceProvider.ComputeHash(Stream)

and just pass in an input stream opened on your file. This method will almost certainly not read in the whole file in memory in one go.

I would also note that in most implementations of MD5 it's possible to add byte[] data into the digest function a chunk at a time, and then ask for the hash at the end.

Alnitak
Matthew Flaschen
This is pretty sweet. I tried simple solutions from other posts about reading to a memorystream, etc, but they all seemed to fail for extremely large files. This works perfectly and it's super simple. Thanks :)
mrduclaw