Last active
January 13, 2017 05:07
-
-
Save songzheng45/b294cfa44ece440c8eea9b50790a3991 to your computer and use it in GitHub Desktop.
C# 计算字符串的 MD5 哈希值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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