MD5Utility.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. namespace assetBundleUtility
  2. {
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. public class MD5Utility
  8. {
  9. public static byte[] GetMD5(string str)
  10. {
  11. byte[] bytes = Encoding.UTF8.GetBytes(str);
  12. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  13. byte[] md5Bytes = md5.ComputeHash(bytes);
  14. return md5Bytes;
  15. }
  16. public static byte[] GetMD5(FileStream fileStream)
  17. {
  18. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  19. byte[] md5Bytes = md5.ComputeHash(fileStream);
  20. return md5Bytes;
  21. }
  22. public static string BytesToString(byte[] bytes)
  23. {
  24. StringBuilder stringBuilder = new StringBuilder();
  25. foreach (var md5Byte in bytes)
  26. {
  27. stringBuilder.Append(md5Byte.ToString("X2")); //X2表示转为大写
  28. }
  29. return stringBuilder.ToString();
  30. }
  31. public static List<string> GetAllMD5StringFromPath(List<string> pathes)
  32. {
  33. List<string> md5Strings = new List<string>();
  34. for (int i = 0; i < pathes.Count; i++)
  35. {
  36. string md5String = GetMD5StringFromPath(pathes[i]);
  37. md5Strings.Add(md5String);
  38. }
  39. return md5Strings;
  40. }
  41. public static string GetMD5StringFromPath(string path)
  42. {
  43. FileStream fileStream = new FileStream(path, FileMode.Open);
  44. byte[] bytes = GetMD5(fileStream);
  45. string md5Strings = BytesToString(bytes);
  46. fileStream.Close();
  47. return md5Strings;
  48. }
  49. }
  50. }