Learning never exhausts the mind

By

This short snippet will generate an MD5 hash of a given string value in C#

Simply pass in a string and the function will return a MD5 hash of the contents.

public string CalculateMD5Hash(string input) 
{ 
  // Calculate MD5 hash from input 
  var md5 = MD5.Create(); 
  byte[] inputBytes = Encoding.ASCII.GetBytes(input); 
  byte[] hash = md5.ComputeHash(inputBytes); 

  // Convert byte array to hex string 
  var sb = new StringBuilder(); 
  for (int i = 0; i < hash.Length; i++) 
  { 
    sb.Append(hash[i].ToString("X2")); 
  } 

  return sb.ToString(); 
}

 

Enjoyed this post? Why not share it!

If you enjoyed this post, please share it with others. Click one of the social media buttons below to share on Facebook, Twitter, LinkedIn, Pinterest or email to a friend.

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.