title: C# High speed MD5/SHA hash over network
body: <p>In a C# project that I am currently working on, we're attempting to calculate the MD5 of a large quantity of files over a network (current pot is 2.7 million, client pot may be in excess of 10 million).  With the number of files that we are processing, speed is of the issue.</p>
  
  <p>The reason we do this is to verify the file was copied to a different location without modification.  </p>
  
  <p>We currently use the following code to calculate the MD5 of a file</p>
  
  <pre><code>MD5 md5 = new MD5CryptoServiceProvider();
  StringBuilder sb = new StringBuilder();
  
  byte[] hashMD5 = null;
  
  try
  {
     // Open stream to file to get MD5 hash for, create hash
     using (FileStream fsMD5 = new FileStream(sFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        hashMD5 = md5.ComputeHash(fsMD5);
  }
  catch (Exception ex)
  {
     clsLogging.logError(clsLogging.ErrorLevel.ERROR, ex);
  }
  
  string md5sum = "";
  if (hashMD5 != null)
  {
     // Change hash into readable text
     foreach (byte hex in hashMD5)
        sb.Append(hex.ToString("x2"));
      md5sum = sb.ToString();
  }
  </code></pre>
  
  <p>However, the speed of this isn't what my manager has been hoping for.  We've gone through a number of changes to the way and number of files that we calculate the MD5 for (i.e. we don't do it for files that we don't copy... until today when my manager changed his mind so <strong>ALL</strong> files must have a MD5 calculated for them, in case at some future time a client wishes to bugger with our program so all files are copied i guess)</p>
  
  <p>I realize that the speed of the network is probably a major contributing factor (100Mbit/s).   Is there an efficient way to calculate the MD5 of the contents of a file over a network?</p>
  
  <p>Thanks in advance.
  Trevor Watson</p>
  
  <p>Edit: put all code in block instead of just a part of it.</p>