MD5Helper.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.IO;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace Fort23.Core
  5. {
  6. public static class MD5Helper
  7. {
  8. public static string FileMD5(string filePath)
  9. {
  10. // if (!filePath.Contains(".manifest"))
  11. // {
  12. // return "";
  13. // }
  14. byte[] retVal;
  15. using (FileStream file = new FileStream(filePath, FileMode.Open))
  16. {
  17. MD5 md5 = new MD5CryptoServiceProvider();
  18. retVal = md5.ComputeHash(file);
  19. }
  20. return ByteArrayToString(retVal);
  21. }
  22. public static string FileMD5(byte[] data)
  23. {
  24. byte[] retVal;
  25. MD5 md5 = new MD5CryptoServiceProvider();
  26. retVal = md5.ComputeHash(data);
  27. return ByteArrayToString(retVal);
  28. }
  29. public static string ByteArrayToString(byte[] ba)
  30. {
  31. StringBuilder hex = new StringBuilder(ba.Length * 2);
  32. foreach (byte b in ba)
  33. {
  34. hex.AppendFormat("{0:x2}", b);
  35. }
  36. return hex.ToString();
  37. }
  38. }
  39. }