MD5Util.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //------------------------------------------------------------------------------
  2. // <auto-generated>
  3. // 此代码由工具生成。
  4. // 运行时版本:4.0.30319.1
  5. //
  6. // 对此文件的更改可能会导致不正确的行为,并且如果
  7. // 重新生成代码,这些更改将会丢失。
  8. // </auto-generated>
  9. //------------------------------------------------------------------------------
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.Security.Cryptography;
  14. using System.IO;
  15. public class MD5Util
  16. {
  17. public static string Encrypt(string input)
  18. {
  19. MD5 md5 = MD5.Create();
  20. byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
  21. byte[] hash = md5.ComputeHash(inputBytes);
  22. StringBuilder sb = new StringBuilder();
  23. for (int i = 0; i < hash.Length; i++)
  24. {
  25. sb.Append(hash[i].ToString("x2"));//x2 lowercase, X2 uppercase
  26. }
  27. return sb.ToString();
  28. }
  29. public static string GetFileHash(string filePath)
  30. {
  31. try
  32. {
  33. FileStream fs = File.Open(filePath, FileMode.Open);
  34. int len = (int)fs.Length;
  35. byte[] data = new byte[len];
  36. fs.Read(data, 0, len);
  37. fs.Close();
  38. fs.Dispose();
  39. MD5 md5 = new MD5CryptoServiceProvider();
  40. byte[] result = md5.ComputeHash(data);
  41. string fileMD5 = "";
  42. foreach (byte b in result)
  43. {
  44. fileMD5 += Convert.ToString(b, 16);
  45. }
  46. return fileMD5;
  47. }
  48. catch (Exception e)
  49. {
  50. Debuger.LogException(e);
  51. return "";
  52. }
  53. }
  54. public static string GetFileHash(byte[] data)
  55. {
  56. try
  57. {
  58. MD5 md5 = new MD5CryptoServiceProvider();
  59. byte[] result = md5.ComputeHash(data);
  60. string fileMD5 = "";
  61. foreach (byte b in result)
  62. {
  63. fileMD5 += Convert.ToString(b, 16);
  64. }
  65. return fileMD5;
  66. }
  67. catch (Exception e)
  68. {
  69. Debuger.LogException(e);
  70. return "";
  71. }
  72. }
  73. }