CryptographyProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 = false;
  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. using (var r = new Rfc2898DeriveBytes(bytes, salt, iterations))
  55. {
  56. return r.GetBytes(32);
  57. }
  58. }
  59. throw new CryptographicException($"Cannot currently use PBKDF2 with requested hash method: {method}");
  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. else if (_supportedHashMethods.Contains(hashMethod))
  69. {
  70. using (var h = HashAlgorithm.Create(hashMethod))
  71. {
  72. if (salt.Length == 0)
  73. {
  74. return h.ComputeHash(bytes);
  75. }
  76. else
  77. {
  78. byte[] salted = new byte[bytes.Length + salt.Length];
  79. Array.Copy(bytes, salted, bytes.Length);
  80. Array.Copy(salt, 0, salted, bytes.Length, salt.Length);
  81. return h.ComputeHash(salted);
  82. }
  83. }
  84. }
  85. throw new CryptographicException($"Requested hash method is not supported: {hashMethod}");
  86. }
  87. /// <inheritdoc />
  88. public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)
  89. => PBKDF2(DefaultHashMethod, bytes, salt, DefaultIterations);
  90. /// <inheritdoc />
  91. public byte[] GenerateSalt()
  92. => GenerateSalt(DefaultSaltLength);
  93. /// <inheritdoc />
  94. public byte[] GenerateSalt(int length)
  95. {
  96. byte[] salt = new byte[length];
  97. _randomNumberGenerator.GetBytes(salt);
  98. return salt;
  99. }
  100. /// <inheritdoc />
  101. public void Dispose()
  102. {
  103. Dispose(true);
  104. GC.SuppressFinalize(this);
  105. }
  106. /// <summary>
  107. /// Releases unmanaged and - optionally - managed resources.
  108. /// </summary>
  109. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  110. protected virtual void Dispose(bool disposing)
  111. {
  112. if (_disposed)
  113. {
  114. return;
  115. }
  116. if (disposing)
  117. {
  118. _randomNumberGenerator.Dispose();
  119. }
  120. _randomNumberGenerator = null;
  121. _disposed = true;
  122. }
  123. }
  124. }