2014/07/29

How to compute MD5 in C# ?

public static string ComputeMd5(string input, bool convert2smallLetters = false)
{
MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
if (!convert2smallLetters)
sb.Append(hash[i].ToString("X2"));
else
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
view raw gistfile1.cs hosted with ❤ by GitHub

No comments:

Post a Comment