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     }

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/13565376.html

posted @ 2020-08-26 15:18  孤独の巡礼  阅读(786)  评论(0编辑  收藏  举报