2
0

CryptographyProvider.cs 4.8 KB

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