Skip to content

Instantly share code, notes, and snippets.

@songzheng45
Last active January 13, 2017 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songzheng45/b294cfa44ece440c8eea9b50790a3991 to your computer and use it in GitHub Desktop.
Save songzheng45/b294cfa44ece440c8eea9b50790a3991 to your computer and use it in GitHub Desktop.
C# 计算字符串的 MD5 哈希值
public string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment