http://msdn.microsoft.com/library/defau ... stopic.asp
http://www.weste.net/2005/4-7/17122137414.html (asp.net 用)
http://www.obviex.com/samples/hash.aspx
以上的內容對於算 hash 方法都能直接使用, 但算完後的值必須能有效轉為字串才方便利用, 則必須配合下篇內容,
http://msdn.microsoft.com/library/defau ... stopic.asp
如何將 byte[] 轉為 hex string 的功能.
整理上面的文章, 寫了一個小 sample 做為計算 md5 hash value 並輸出 string 的方法,
代碼: 選擇全部
public static string MD5ToString(string val)
{
byte[] bytearray1 = Encoding.ASCII.GetBytes(val);
System.Security.Cryptography.MD5CryptoServiceProvider amd5 = new MD5CryptoServiceProvider();
byte[] bytearray2 = amd5.ComputeHash(bytearray1);
return ToHexString(bytearray2);
}
static char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static string ToHexString(byte[] bytes)
{
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}