CryptographyProvider.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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
  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. public string DefaultHashMethod => "PBKDF2";
  30. private RandomNumberGenerator _randomNumberGenerator;
  31. private const int _defaultIterations = 1000;
  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 Guid GetMD5(string str)
  41. {
  42. return new Guid(ComputeMD5(Encoding.Unicode.GetBytes(str)));
  43. }
  44. public byte[] ComputeSHA1(byte[] bytes)
  45. {
  46. using (var provider = SHA1.Create())
  47. {
  48. return provider.ComputeHash(bytes);
  49. }
  50. }
  51. public byte[] ComputeMD5(Stream str)
  52. {
  53. using (var provider = MD5.Create())
  54. {
  55. return provider.ComputeHash(str);
  56. }
  57. }
  58. public byte[] ComputeMD5(byte[] bytes)
  59. {
  60. using (var provider = MD5.Create())
  61. {
  62. return provider.ComputeHash(bytes);
  63. }
  64. }
  65. public IEnumerable<string> GetSupportedHashMethods()
  66. {
  67. return _supportedHashMethods;
  68. }
  69. private byte[] PBKDF2(string method, byte[] bytes, byte[] salt, int iterations)
  70. {
  71. //downgrading for now as we need this library to be dotnetstandard compliant
  72. //with this downgrade we'll add a check to make sure we're on the downgrade method at the moment
  73. if (method == DefaultHashMethod)
  74. {
  75. using (var r = new Rfc2898DeriveBytes(bytes, salt, iterations))
  76. {
  77. return r.GetBytes(32);
  78. }
  79. }
  80. throw new CryptographicException($"Cannot currently use PBKDF2 with requested hash method: {method}");
  81. }
  82. public byte[] ComputeHash(string hashMethod, byte[] bytes)
  83. {
  84. return ComputeHash(hashMethod, bytes, Array.Empty<byte>());
  85. }
  86. public byte[] ComputeHashWithDefaultMethod(byte[] bytes)
  87. {
  88. return ComputeHash(DefaultHashMethod, bytes);
  89. }
  90. public byte[] ComputeHash(string hashMethod, byte[] bytes, byte[] salt)
  91. {
  92. if (hashMethod == DefaultHashMethod)
  93. {
  94. return PBKDF2(hashMethod, bytes, salt, _defaultIterations);
  95. }
  96. else if (_supportedHashMethods.Contains(hashMethod))
  97. {
  98. using (var h = HashAlgorithm.Create(hashMethod))
  99. {
  100. if (salt.Length == 0)
  101. {
  102. return h.ComputeHash(bytes);
  103. }
  104. else
  105. {
  106. byte[] salted = new byte[bytes.Length + salt.Length];
  107. Array.Copy(bytes, salted, bytes.Length);
  108. Array.Copy(salt, 0, salted, bytes.Length, salt.Length);
  109. return h.ComputeHash(salted);
  110. }
  111. }
  112. }
  113. else
  114. {
  115. throw new CryptographicException($"Requested hash method is not supported: {hashMethod}");
  116. }
  117. }
  118. public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)
  119. {
  120. return PBKDF2(DefaultHashMethod, bytes, salt, _defaultIterations);
  121. }
  122. public byte[] ComputeHash(PasswordHash hash)
  123. {
  124. int iterations = _defaultIterations;
  125. if (!hash.Parameters.ContainsKey("iterations"))
  126. {
  127. hash.Parameters.Add("iterations", _defaultIterations.ToString(CultureInfo.InvariantCulture));
  128. }
  129. else
  130. {
  131. try
  132. {
  133. iterations = int.Parse(hash.Parameters["iterations"]);
  134. }
  135. catch (Exception e)
  136. {
  137. throw new InvalidDataException($"Couldn't successfully parse iterations value from string: {hash.Parameters["iterations"]}", e);
  138. }
  139. }
  140. return PBKDF2(hash.Id, hash.HashBytes, hash.SaltBytes, iterations);
  141. }
  142. public byte[] GenerateSalt()
  143. {
  144. byte[] salt = new byte[64];
  145. _randomNumberGenerator.GetBytes(salt);
  146. return salt;
  147. }
  148. }
  149. }