MD5.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.IO;
  3. namespace LJLib.Tools.Encry
  4. {
  5. public class MD5
  6. {
  7. public byte[] ComputeHash(byte[] bytes)
  8. {
  9. using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
  10. {
  11. return md5.ComputeHash(bytes);
  12. }
  13. }
  14. public byte[] ComputeHash(byte[] bytes, int offset, int count)
  15. {
  16. using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
  17. {
  18. return md5.ComputeHash(bytes, offset, count);
  19. }
  20. }
  21. public byte[] ComputeHash(System.IO.Stream inputStream)
  22. {
  23. using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
  24. {
  25. return md5.ComputeHash(inputStream);
  26. }
  27. }
  28. public string GetMD5(byte[] bytes)
  29. {
  30. byte[] rslt = this.ComputeHash(bytes);
  31. string str_rslt = BitConverter.ToString(rslt);
  32. str_rslt = str_rslt.Replace("-", "");
  33. return str_rslt;
  34. }
  35. public string GetMD5(byte[] bytes, int offset, int count)
  36. {
  37. byte[] rslt = this.ComputeHash(bytes, offset, count);
  38. string str_rslt = BitConverter.ToString(rslt);
  39. str_rslt = str_rslt.Replace("-", "");
  40. return str_rslt;
  41. }
  42. public string GetMD5(System.IO.Stream inputStream)
  43. {
  44. byte[] rslt = this.ComputeHash(inputStream);
  45. string str_rslt = BitConverter.ToString(rslt);
  46. str_rslt = str_rslt.Replace("-", "");
  47. return str_rslt;
  48. }
  49. public string GetMD5(string filename)
  50. {
  51. using (var fs = System.IO.File.OpenRead(filename))
  52. {
  53. return GetMD5(fs);
  54. }
  55. }
  56. }
  57. }