CryptographyProvider.cs 4.8 KB

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