c# - Possible to calculate MD5 (or other) hash with buffered reads? -


I need to calculate the checksum of large files (gigabytes). It can be completed using the following method Is:

  private byte [] calcHash (string file) {System.Security.Cryptography.HashAlgorithm ha = System.Security.Cryptography.MD5.Create (); File stream fs = new filestream (file, filemodel open, fileaccess read.); Byte [] hash is =. Compact hash (FS); Fs.Close (); Return hash; }  

However, files are usually written in a buffer manner (32MB at a time writes) I am very confident that I saw the override of a hash function Which allows me to write MD5 (or other) hash at the same time, i.e.: calculating the hash of a buffer, feeds the hash resulting in the next strike.

Something like this: (Pseudocode-IIS)

  byte [] hash = new byte [] {0,0,0,0,0,0,0,0,0 , 0}; While (! Eof) {buffer = readFromSourceFile (); WriteFile (buffer); Hash = calash (buffer, hash); }  

What's going to be done by running the ChessShash function on the hash file now?

Now, I can not get any override in this way 3.5 3.5 framework, am I dreaming? Has it ever existed, or am I just lousy in search? The reason for calculating both writing and checksum at one go is that due to the large files it makes sense

< P> You use the TransformBlock and TransformFinalBlock methods to process the data in the chunks.

  // init MD5 md5 = MD5.Create (); Int offset = 0; // For each block: offset + = md5.TransformBlock (block, 0, block length, block, 0); // For the last block: md5.TransformFinalBlock (block, 0, block.); // Get a byte with code [] hash = MD5. Hash;   

Note: This works to send all the code to TransformBlock (with at least MD5 providers) and then an empty block to TransformFinalBlock Send to to finalize the process.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -