No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

62 líneas
2.1 KiB

  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. }