CryptographyProvider.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. public CryptographyProvider()
  30. {
  31. // FIXME: When we get DotNet Standard 2.1 we need to revisit how we do the crypto
  32. // Currently supported hash methods from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=netcore-2.1
  33. // there might be a better way to autogenerate this list as dotnet updates, but I couldn't find one
  34. // 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
  35. _randomNumberGenerator = RandomNumberGenerator.Create();
  36. }
  37. public string DefaultHashMethod => "PBKDF2";
  38. public IEnumerable<string> GetSupportedHashMethods()
  39. => _supportedHashMethods;
  40. private byte[] PBKDF2(string method, byte[] bytes, byte[] salt, int iterations)
  41. {
  42. // downgrading for now as we need this library to be dotnetstandard compliant
  43. // with this downgrade we'll add a check to make sure we're on the downgrade method at the moment
  44. if (method == DefaultHashMethod)
  45. {
  46. using (var r = new Rfc2898DeriveBytes(bytes, salt, iterations))
  47. {
  48. return r.GetBytes(32);
  49. }
  50. }
  51. throw new CryptographicException($"Cannot currently use PBKDF2 with requested hash method: {method}");
  52. }
  53. public byte[] ComputeHash(string hashMethod, byte[] bytes)
  54. => ComputeHash(hashMethod, bytes, Array.Empty<byte>());
  55. public byte[] ComputeHashWithDefaultMethod(byte[] bytes)
  56. => ComputeHash(DefaultHashMethod, bytes);
  57. public byte[] ComputeHash(string hashMethod, byte[] bytes, byte[] salt)
  58. {
  59. if (hashMethod == DefaultHashMethod)
  60. {
  61. return PBKDF2(hashMethod, bytes, salt, DefaultIterations);
  62. }
  63. else if (_supportedHashMethods.Contains(hashMethod))
  64. {
  65. using (var h = HashAlgorithm.Create(hashMethod))
  66. {
  67. if (salt.Length == 0)
  68. {
  69. return h.ComputeHash(bytes);
  70. }
  71. else
  72. {
  73. byte[] salted = new byte[bytes.Length + salt.Length];
  74. Array.Copy(bytes, salted, bytes.Length);
  75. Array.Copy(salt, 0, salted, bytes.Length, salt.Length);
  76. return h.ComputeHash(salted);
  77. }
  78. }
  79. }
  80. throw new CryptographicException($"Requested hash method is not supported: {hashMethod}");
  81. }
  82. public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)
  83. => PBKDF2(DefaultHashMethod, bytes, salt, DefaultIterations);
  84. public byte[] GenerateSalt()
  85. => GenerateSalt(DefaultSaltLength);
  86. public byte[] GenerateSalt(int length)
  87. {
  88. byte[] salt = new byte[length];
  89. _randomNumberGenerator.GetBytes(salt);
  90. return salt;
  91. }
  92. /// <inheritdoc />
  93. public void Dispose()
  94. {
  95. Dispose(true);
  96. GC.SuppressFinalize(this);
  97. }
  98. protected virtual void Dispose(bool disposing)
  99. {
  100. if (_disposed)
  101. {
  102. return;
  103. }
  104. if (disposing)
  105. {
  106. _randomNumberGenerator.Dispose();
  107. }
  108. _randomNumberGenerator = null;
  109. _disposed = true;
  110. }
  111. }
  112. }