Skip to content

Instantly share code, notes, and snippets.

@mfloryan
Created August 16, 2011 13:55
Show Gist options
  • Select an option

  • Save mfloryan/1149138 to your computer and use it in GitHub Desktop.

Select an option

Save mfloryan/1149138 to your computer and use it in GitHub Desktop.
MD5 for Strings in C#
static string getMd5Hash(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment