title: Which cryptographic hash function should I choose?
body: <p>The .NET framework ships with 6 different hashing algorithms:</p>
  <ul>
  <li>MD5: 16 bytes (Time to hash 500MB: 1462 ms)</li>
  <li>SHA-1: 20 bytes (1644 ms)</li>
  <li>SHA256: 32 bytes (5618 ms)</li>
  <li>SHA384: 48 bytes (3839 ms)</li>
  <li>SHA512: 64 bytes (3820 ms)</li>
  <li>RIPEMD: 20 bytes (7066 ms)</li>
  </ul>
  <p>Each of these functions performs differently; MD5 being the fastest and RIPEMD being the slowest.</p>
  <p>MD5 has the advantage that it fits in the built-in Guid type; <a href="https://www.rfc-editor.org/rfc/rfc4122#section-4.3" rel="nofollow noreferrer">and it is the basis of the type 3 UUID</a>. <a href="https://www.rfc-editor.org/rfc/rfc4122#section-4.3" rel="nofollow noreferrer">SHA-1 hash is the basis of type 5 UUID.</a> Which makes them really easy to use for identification.</p>
  <p>MD5 however is vulnerable to <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function" rel="nofollow noreferrer">collision attacks</a>, SHA-1 is also vulnerable but to a lesser degree.</p>
  <h3>
  Under what conditions should I use which hashing algorithm?
  </h3>
  <p>Particular questions I'm really curious to see answered are:</p>
  <ul>
  <li><p>Is MD5 not to be trusted? Under normal situations when you use the MD5 algorithm with no malicious intent and no third party has any malicious intent would you expect ANY collisions (meaning two arbitrary byte[] producing the same hash)</p>
  </li>
  <li><p>How much better is RIPEMD than SHA1? (if its any better) its 5 times slower to compute but the hash size is the same as SHA1.</p>
  </li>
  <li><p>What are the odds of getting non-malicious collisions when hashing file-names (or other short strings)? (Eg. 2 random file-names with same MD5 hash) (with MD5 / SHA1 / SHA2xx) In general what are the odds for non-malicious collisions?</p>
  </li>
  </ul>
  <p>This is the benchmark I used:</p>
  <pre><code>    static void TimeAction(string description, int iterations, Action func) {
          var watch = new Stopwatch();
          watch.Start();
          for (int i = 0; i &lt; iterations; i++) {
              func();
          }
          watch.Stop();
          Console.Write(description);
          Console.WriteLine(&quot; Time Elapsed {0} ms&quot;, watch.ElapsedMilliseconds);
      }
  
      static byte[] GetRandomBytes(int count) {
          var bytes = new byte[count];
          (new Random()).NextBytes(bytes);
          return bytes;
      }
      
  
      static void Main(string[] args) {
  
          var md5 = new MD5CryptoServiceProvider();
          var sha1 = new SHA1CryptoServiceProvider();
          var sha256 = new SHA256CryptoServiceProvider();
          var sha384 = new SHA384CryptoServiceProvider();
          var sha512 = new SHA512CryptoServiceProvider();
          var ripemd160 = new RIPEMD160Managed();
  
          var source = GetRandomBytes(1000 * 1024);
  
          var algorithms = new Dictionary&lt;string,HashAlgorithm&gt;();
          algorithms[&quot;md5&quot;] = md5;
          algorithms[&quot;sha1&quot;] = sha1;
          algorithms[&quot;sha256&quot;] = sha256;
          algorithms[&quot;sha384&quot;] = sha384;
          algorithms[&quot;sha512&quot;] = sha512;
          algorithms[&quot;ripemd160&quot;] = ripemd160;
  
          foreach (var pair in algorithms) {
              Console.WriteLine(&quot;Hash Length for {0} is {1}&quot;, 
                  pair.Key, 
                  pair.Value.ComputeHash(source).Length);
          }
  
          foreach (var pair in algorithms) {
              TimeAction(pair.Key + &quot; calculation&quot;, 500, () =&gt;
              {
                  pair.Value.ComputeHash(source);
              });
          }
  
          Console.ReadKey();
      }
  </code></pre>