123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace ET
- {
- public static class MD5Helper
- {
- /// <summary>
- /// 获取文件md5
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- public static string FileMD5(string filePath)
- {
- byte[] retVal;
- using (FileStream file = new FileStream(filePath, FileMode.Open))
- {
- MD5 md5 = MD5.Create();
- retVal = md5.ComputeHash(file);
- }
- return retVal.ToHex("x2");
- }
-
- /// <summary>
- /// 获取字符串md5
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string StringMD5(string str)
- {
- byte[] retVal = Encoding.GetEncoding("UTF-8").GetBytes(str);
- byte[] hashValue = MD5.Create().ComputeHash(retVal);
- StringBuilder tmp = new StringBuilder();
- foreach (byte i in hashValue)
- {
- tmp.Append(i.ToString("x2"));
- }
- return tmp.ToString();
- }
- }
- }
|