标签:app dig 实现 linq gen 碰撞 c# pac utf8
什么是MD5?
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致。是计算机广泛使用的杂凑算法之一,主流编程语言普遍已有MD5实现。
MD5的特点:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Security.Cryptography; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace ConsoleApplication1 10 { 11 public class MD5Helper 12 { 13 /// <summary> 14 /// 计算字节数组的 MD5 值 15 /// </summary> 16 /// <param name="buffer"></param> 17 /// <returns></returns> 18 public static string CalcMD5(byte[] buffer) 19 { 20 using (MD5 md5 = MD5.Create()) 21 { 22 byte[] md5Bytes = md5.ComputeHash(buffer); 23 return BytesToString(md5Bytes); 24 } 25 } 26 27 /// <summary> 28 /// 将得到的 MD5 字节数组转成 字符串 29 /// </summary> 30 /// <param name="md5Bytes"></param> 31 /// <returns></returns> 32 private static string BytesToString(byte[] md5Bytes) 33 { 34 StringBuilder sb = new StringBuilder(); 35 for (int i = 0; i < md5Bytes.Length; i++) 36 { 37 sb.Append(md5Bytes[i].ToString("X2")); 38 } 39 return sb.ToString(); 40 } 41 42 /// <summary> 43 /// 计算字符串的 MD5 值 44 /// </summary> 45 /// <param name="str"></param> 46 /// <returns></returns> 47 public static string CalcMD5(string str) 48 { 49 byte[] buffer = Encoding.UTF8.GetBytes(str); 50 return CalcMD5(buffer); 51 } 52 53 /// <summary> 54 /// 计算流的 MD5 值 55 /// </summary> 56 /// <param name="stream"></param> 57 /// <returns></returns> 58 public static string CalcMD5(Stream stream) 59 { 60 using (MD5 md5 = MD5.Create()) 61 { 62 byte[] buffer = md5.ComputeHash(stream); 63 return BytesToString(buffer); 64 } 65 } 66 } 67 }
标签:app dig 实现 linq gen 碰撞 c# pac utf8
原文地址:http://www.cnblogs.com/yesunda/p/6765523.html