c# 之如何计算md5码
代码如下:
1 static MD5 s_md5; 2 3 static MD5 MD5Obj => s_md5 ??= MD5.Create(); 4 5 6 public static string ComputeHash(byte[] buffer) 7 { 8 if (buffer == null || buffer.Length < 1) 9 return ""; 10 11 byte[] hash = MD5Obj.ComputeHash(buffer); 12 StringBuilder sb = new StringBuilder(); 13 14 foreach (var b in hash) 15 sb.Append(b.ToString("x2")); 16 17 return sb.ToString(); 18 } 19 20 public static string ComputeHash(string str) 21 { 22 byte[] buffer = Encoding.UTF8.GetBytes(str); 23 return ComputeHash(buffer); 24 }