CryptographyProvider.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography;
  4. using MediaBrowser.Model.Cryptography;
  5. using static MediaBrowser.Common.Cryptography.Constants;
  6. namespace Emby.Server.Implementations.Cryptography
  7. {
  8. public class CryptographyProvider : ICryptoProvider, IDisposable
  9. {
  10. private static readonly HashSet<string> _supportedHashMethods = new HashSet<string>()
  11. {
  12. "MD5",
  13. "System.Security.Cryptography.MD5",
  14. "SHA",
  15. "SHA1",
  16. "System.Security.Cryptography.SHA1",
  17. "SHA256",
  18. "SHA-256",
  19. "System.Security.Cryptography.SHA256",
  20. "SHA384",
  21. "SHA-384",
  22. "System.Security.Cryptography.SHA384",
  23. "SHA512",
  24. "SHA-512",
  25. "System.Security.Cryptography.SHA512"
  26. };
  27. private RandomNumberGenerator _randomNumberGenerator;
  28. private bool _disposed = false;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="CryptographyProvider"/> class.
  31. /// </summary>
  32. public CryptographyProvider()
  33. {
  34. // FIXME: When we get DotNet Standard 2.1 we need to revisit how we do the crypto
  35. // Currently supported hash methods from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=netcore-2.1
  36. // there might be a better way to autogenerate this list as dotnet updates, but I couldn't find one
  37. // Please note the default method of PBKDF2 is not included, it cannot be used to generate hashes cleanly as it is actually a pbkdf with sha1
  38. _randomNumberGenerator = RandomNumberGenerator.Create();
  39. }
  40. public string DefaultHashMethod => "PBKDF2";
  41. public IEnumerable<string> GetSupportedHashMethods()
  42. => _supportedHashMethods;
  43. private byte[] PBKDF2(string method, byte[] bytes, byte[] salt, int iterations)
  44. {
  45. // downgrading for now as we need this library to be dotnetstandard compliant
  46. // with this downgrade we'll add a check to make sure we're on the downgrade method at the moment
  47. if (method == DefaultHashMethod)
  48. {
  49. using (var r = new Rfc2898DeriveBytes(bytes, salt, iterations))
  50. {
  51. return r.GetBytes(32);
  52. }
  53. }
  54. throw new CryptographicException($"Cannot currently use PBKDF2 with requested hash method: {method}");
  55. }
  56. public byte[] ComputeHash(string hashMethod, byte[] bytes, byte[] salt)
  57. {
  58. if (hashMethod == DefaultHashMethod)
  59. {
  60. return PBKDF2(hashMethod, bytes, salt, DefaultIterations);
  61. }
  62. else if (_supportedHashMethods.Contains(hashMethod))
  63. {
  64. using (var h = HashAlgorithm.Create(hashMethod))
  65. {
  66. if (salt.Length == 0)
  67. {
  68. return h.ComputeHash(bytes);
  69. }
  70. else
  71. {
  72. byte[] salted = new byte[bytes.Length + salt.Length];
  73. Array.Copy(bytes, salted, bytes.Length);
  74. Array.Copy(salt, 0, salted, bytes.Length, salt.Length);
  75. return h.ComputeHash(salted);
  76. }
  77. }
  78. }
  79. throw new CryptographicException($"Requested hash method is not supported: {hashMethod}");
  80. }
  81. public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)
  82. => PBKDF2(DefaultHashMethod, bytes, salt, DefaultIterations);
  83. public byte[] GenerateSalt()
  84. => GenerateSalt(DefaultSaltLength);
  85. public byte[] GenerateSalt(int length)
  86. {
  87. byte[] salt = new byte[length];
  88. _randomNumberGenerator.GetBytes(salt);
  89. return salt;
  90. }
  91. /// <inheritdoc />
  92. public void Dispose()
  93. {
  94. Dispose(true);
  95. GC.SuppressFinalize(this);
  96. }
  97. protected virtual void Dispose(bool disposing)
  98. {
  99. if (_disposed)
  100. {
  101. return;
  102. }
  103. if (disposing)
  104. {
  105. _randomNumberGenerator.Dispose();
  106. }
  107. _randomNumberGenerator = null;
  108. _disposed = true;
  109. }
  110. }
  111. }