Creating an MD5 String Hash

Below is code that will easily hash a string using the MD5 encryption. This is typically used when performing data integrity checks.

using System.Security.Cryptography;
using System.Text;

namespace MyApp.Caching
{
   public static class CachingHelper
   {
      static public string CreateMD5Hash(string str)
      {
         var unicodeText = new byte[str.Length * 2];
         Encoding.Unicode.GetEncoder().GetBytes(str.ToCharArray(), 0, 
                                     str.Length, unicodeText, 0, true);

         using (var cacheProvider = new MD5CryptoServiceProvider())
         {
            var result = cacheProvider.ComputeHash(unicodeText);
            var sb = new StringBuilder(result.Length * 2);
            for (int i = 0; i < result.Length; i++)
            {
               sb.Append(result[i].ToString("X2"));
            }
            return sb.ToString();
         }
      }
   }
}

The resutling hash looks something like this:

52B567DE3B72669CD9B178EEE9336E70

Tip By : David McCarter