CryptographyProvider.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using MediaBrowser.Model.Cryptography;
  8. namespace Emby.Server.Implementations.Cryptography
  9. {
  10. public class CryptographyProvider : ICryptoProvider, IDisposable
  11. {
  12. private static readonly HashSet<string> _supportedHashMethods = new HashSet<string>()
  13. {
  14. "MD5",
  15. "System.Security.Cryptography.MD5",
  16. "SHA",
  17. "SHA1",
  18. "System.Security.Cryptography.SHA1",
  19. "SHA256",
  20. "SHA-256",
  21. "System.Security.Cryptography.SHA256",
  22. "SHA384",
  23. "SHA-384",
  24. "System.Security.Cryptography.SHA384",
  25. "SHA512",
  26. "SHA-512",
  27. "System.Security.Cryptography.SHA512"
  28. };
  29. private RandomNumberGenerator _randomNumberGenerator;
  30. private const int _defaultIterations = 1000;
  31. private bool _disposed = false;
  32. public CryptographyProvider()
  33. {
  34. // FIXME: When we get DotNet Standard 2.1 we need to revisit how we do the crypto
  35. // Currently supported hash methods from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=netcore-2.1
  36. // there might be a better way to autogenerate this list as dotnet updates, but I couldn't find one
  37. // 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
  38. _randomNumberGenerator = RandomNumberGenerator.Create();
  39. }
  40. public string DefaultHashMethod => "PBKDF2";
  41. [Obsolete("Use System.Security.Cryptography.MD5 directly")]
  42. public Guid GetMD5(string str)
  43. => new Guid(ComputeMD5(Encoding.Unicode.GetBytes(str)));
  44. [Obsolete("Use System.Security.Cryptography.SHA1 directly")]
  45. public byte[] ComputeSHA1(byte[] bytes)
  46. {
  47. using (var provider = SHA1.Create())
  48. {
  49. return provider.ComputeHash(bytes);
  50. }
  51. }
  52. [Obsolete("Use System.Security.Cryptography.MD5 directly")]
  53. public byte[] ComputeMD5(Stream str)
  54. {
  55. using (var provider = MD5.Create())
  56. {
  57. return provider.ComputeHash(str);
  58. }
  59. }
  60. [Obsolete("Use System.Security.Cryptography.MD5 directly")]
  61. public byte[] ComputeMD5(byte[] bytes)
  62. {
  63. using (var provider = MD5.Create())
  64. {
  65. return provider.ComputeHash(bytes);
  66. }
  67. }
  68. public IEnumerable<string> GetSupportedHashMethods()
  69. => _supportedHashMethods;
  70. private byte[] PBKDF2(string method, byte[] bytes, byte[] salt, int iterations)
  71. {
  72. //downgrading for now as we need this library to be dotnetstandard compliant
  73. //with this downgrade we'll add a check to make sure we're on the downgrade method at the moment
  74. if (method == DefaultHashMethod)
  75. {
  76. using (var r = new Rfc2898DeriveBytes(bytes, salt, iterations))
  77. {
  78. return r.GetBytes(32);
  79. }
  80. }
  81. throw new CryptographicException($"Cannot currently use PBKDF2 with requested hash method: {method}");
  82. }
  83. public byte[] ComputeHash(string hashMethod, byte[] bytes)
  84. => ComputeHash(hashMethod, bytes, Array.Empty<byte>());
  85. public byte[] ComputeHashWithDefaultMethod(byte[] bytes)
  86. => ComputeHash(DefaultHashMethod, bytes);
  87. public byte[] ComputeHash(string hashMethod, byte[] bytes, byte[] salt)
  88. {
  89. if (hashMethod == DefaultHashMethod)
  90. {
  91. return PBKDF2(hashMethod, bytes, salt, _defaultIterations);
  92. }
  93. else if (_supportedHashMethods.Contains(hashMethod))
  94. {
  95. using (var h = HashAlgorithm.Create(hashMethod))
  96. {
  97. if (salt.Length == 0)
  98. {
  99. return h.ComputeHash(bytes);
  100. }
  101. else
  102. {
  103. byte[] salted = new byte[bytes.Length + salt.Length];
  104. Array.Copy(bytes, salted, bytes.Length);
  105. Array.Copy(salt, 0, salted, bytes.Length, salt.Length);
  106. return h.ComputeHash(salted);
  107. }
  108. }
  109. }
  110. throw new CryptographicException($"Requested hash method is not supported: {hashMethod}");
  111. }
  112. public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)
  113. => PBKDF2(DefaultHashMethod, bytes, salt, _defaultIterations);
  114. public byte[] ComputeHash(PasswordHash hash)
  115. {
  116. int iterations = _defaultIterations;
  117. if (!hash.Parameters.ContainsKey("iterations"))
  118. {
  119. hash.Parameters.Add("iterations", iterations.ToString(CultureInfo.InvariantCulture));
  120. }
  121. else if (!int.TryParse(hash.Parameters["iterations"], out iterations))
  122. {
  123. throw new InvalidDataException($"Couldn't successfully parse iterations value from string: {hash.Parameters["iterations"]}");
  124. }
  125. return PBKDF2(hash.Id, hash.Hash, hash.Salt, iterations);
  126. }
  127. public byte[] GenerateSalt()
  128. {
  129. byte[] salt = new byte[64];
  130. _randomNumberGenerator.GetBytes(salt);
  131. return salt;
  132. }
  133. /// <inheritdoc />
  134. public void Dispose()
  135. {
  136. Dispose(true);
  137. GC.SuppressFinalize(this);
  138. }
  139. protected virtual void Dispose(bool disposing)
  140. {
  141. if (_disposed)
  142. {
  143. return;
  144. }
  145. if (disposing)
  146. {
  147. _randomNumberGenerator.Dispose();
  148. }
  149. _randomNumberGenerator = null;
  150. _disposed = true;
  151. }
  152. }
  153. }