CryptographyProvider.cs 4.8 KB

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