You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 8 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Text;
  2. namespace Security2.FileSystem.FileInfo
  3. {
  4. public enum HashAlgorithms
  5. {
  6. SHA1,
  7. SHA256,
  8. SHA384,
  9. SHA512,
  10. MACTripleDES,
  11. MD5,
  12. RIPEMD160
  13. }
  14. public static class Extensions
  15. {
  16. public static string GetHash(this Alphaleonis.Win32.Filesystem.FileInfo file, HashAlgorithms algorithm)
  17. {
  18. byte[] hash = null;
  19. using (var fileStream = file.OpenRead())
  20. {
  21. switch (algorithm)
  22. {
  23. case HashAlgorithms.MD5:
  24. hash = System.Security.Cryptography.MD5.Create().ComputeHash(fileStream);
  25. break;
  26. case HashAlgorithms.SHA1:
  27. hash = System.Security.Cryptography.SHA1.Create().ComputeHash(fileStream);
  28. break;
  29. case HashAlgorithms.SHA256:
  30. hash = System.Security.Cryptography.SHA256.Create().ComputeHash(fileStream);
  31. break;
  32. case HashAlgorithms.SHA384:
  33. hash = System.Security.Cryptography.SHA384.Create().ComputeHash(fileStream);
  34. break;
  35. case HashAlgorithms.SHA512:
  36. hash = System.Security.Cryptography.SHA512.Create().ComputeHash(fileStream);
  37. break;
  38. case HashAlgorithms.MACTripleDES:
  39. hash = System.Security.Cryptography.MACTripleDES.Create().ComputeHash(fileStream);
  40. break;
  41. case HashAlgorithms.RIPEMD160:
  42. hash = System.Security.Cryptography.RIPEMD160.Create().ComputeHash(fileStream);
  43. break;
  44. }
  45. fileStream.Close();
  46. }
  47. var sb = new StringBuilder(hash.Length);
  48. for (var i = 0; i < hash.Length; i++)
  49. {
  50. sb.Append(hash[i].ToString("X2"));
  51. }
  52. return sb.ToString();
  53. }
  54. }
  55. }