I have a program which is going to be used on very large files (current test data is 250GB). I need to be able to calculate both MD5 and SHA1 hashes for these files. Currently my code drops the stream into MD5.Create().ComputeHash(Stream stream), and then the same for SHA1. These, as far as I can tell, read the file in 4096-byte blocks to a buffer internal to the hashing function, until the end of the stream.
The problem is, doing this one after the other takes a VERY long time! Is there any way I can take data into a buffer and provide the buffer to BOTH algorithms before reading a new block into the buffer?
Please explain thoroughly as I’m not an experienced coder.
Sure. You can call
TransformBlockrepeatedly, and thenTransformFinalBlockat the end and then useHashto get the final hash. So something like:The
MD5Cng.CreateandSHA1Cng.Createcalls will create wrappers around native implementations which are likely to be faster than the implementations returned byMD5.CreateandSHA1.Create, but which will be a bit less portable (e.g. for PCLs).