MD5Helper.cs 725 B

1234567891011121314151617181920212223242526272829303132
  1. using System.IO;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace ET
  5. {
  6. public static class MD5Helper
  7. {
  8. public static string FileMD5(string filePath)
  9. {
  10. byte[] retVal;
  11. using (FileStream file = new FileStream(filePath, FileMode.Open))
  12. {
  13. MD5 md5 = MD5.Create();
  14. retVal = md5.ComputeHash(file);
  15. }
  16. return retVal.ToHex("x2");
  17. }
  18. public static string StringMD5(string str)
  19. {
  20. byte[] retVal = Encoding.GetEncoding("UTF-8").GetBytes(str);
  21. byte[] hashValue = MD5.Create().ComputeHash(retVal);
  22. StringBuilder tmp = new StringBuilder();
  23. foreach (byte i in hashValue)
  24. {
  25. tmp.Append(i.ToString("x2"));
  26. }
  27. return tmp.ToString();
  28. }
  29. }
  30. }