Web Design that taps into the haromny and vision of your dreams.

Generate MD5 Hash of String

Generate an MD5 hash of a given string

By on in Coding

57 words, estimated reading time 1 minutes.

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

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(); 
}

Last updated on: Wednesday 21st June 2017

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.