CryptographyProvider.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 HashSet<string> SupportedHashMethods;
  13. public string DefaultHashMethod => "SHA256";
  14. private RandomNumberGenerator rng;
  15. private int defaultiterations = 1000;
  16. public CryptographyProvider()
  17. {
  18. //Currently supported hash methods from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=netcore-2.1
  19. //there might be a better way to autogenerate this list as dotnet updates, but I couldn't find one
  20. SupportedHashMethods = new HashSet<string>()
  21. {
  22. "MD5"
  23. ,"System.Security.Cryptography.MD5"
  24. ,"SHA"
  25. ,"SHA1"
  26. ,"System.Security.Cryptography.SHA1"
  27. ,"SHA256"
  28. ,"SHA-256"
  29. ,"System.Security.Cryptography.SHA256"
  30. ,"SHA384"
  31. ,"SHA-384"
  32. ,"System.Security.Cryptography.SHA384"
  33. ,"SHA512"
  34. ,"SHA-512"
  35. ,"System.Security.Cryptography.SHA512"
  36. };
  37. rng = RandomNumberGenerator.Create();
  38. }
  39. public Guid GetMD5(string str)
  40. {
  41. return new Guid(ComputeMD5(Encoding.Unicode.GetBytes(str)));
  42. }
  43. public byte[] ComputeSHA1(byte[] bytes)
  44. {
  45. using (var provider = SHA1.Create())
  46. {
  47. return provider.ComputeHash(bytes);
  48. }
  49. }
  50. public byte[] ComputeMD5(Stream str)
  51. {
  52. using (var provider = MD5.Create())
  53. {
  54. return provider.ComputeHash(str);
  55. }
  56. }
  57. public byte[] ComputeMD5(byte[] bytes)
  58. {
  59. using (var provider = MD5.Create())
  60. {
  61. return provider.ComputeHash(bytes);
  62. }
  63. }
  64. public IEnumerable<string> GetSupportedHashMethods()
  65. {
  66. return SupportedHashMethods;
  67. }
  68. private byte[] PBKDF2(string method, byte[] bytes, byte[] salt, int iterations)
  69. {
  70. //downgrading for now as we need this library to be dotnetstandard compliant
  71. using (var r = new Rfc2898DeriveBytes(bytes, salt, iterations))
  72. {
  73. return r.GetBytes(32);
  74. }
  75. }
  76. public byte[] ComputeHash(string HashMethod, byte[] bytes)
  77. {
  78. return ComputeHash(HashMethod, bytes, new byte[0]);
  79. }
  80. public byte[] ComputeHashWithDefaultMethod(byte[] bytes)
  81. {
  82. return ComputeHash(DefaultHashMethod, bytes);
  83. }
  84. public byte[] ComputeHash(string HashMethod, byte[] bytes, byte[] salt)
  85. {
  86. if (SupportedHashMethods.Contains(HashMethod))
  87. {
  88. if (salt.Length == 0)
  89. {
  90. using (var h = HashAlgorithm.Create(HashMethod))
  91. {
  92. return h.ComputeHash(bytes);
  93. }
  94. }
  95. else
  96. {
  97. return PBKDF2(HashMethod, bytes, salt, defaultiterations);
  98. }
  99. }
  100. else
  101. {
  102. throw new CryptographicException($"Requested hash method is not supported: {HashMethod}");
  103. }
  104. }
  105. public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)
  106. {
  107. return PBKDF2(DefaultHashMethod, bytes, salt, defaultiterations);
  108. }
  109. public byte[] ComputeHash(PasswordHash hash)
  110. {
  111. int iterations = defaultiterations;
  112. if (!hash.Parameters.ContainsKey("iterations"))
  113. {
  114. hash.Parameters.Add("iterations", defaultiterations.ToString(CultureInfo.InvariantCulture));
  115. }
  116. else
  117. {
  118. try
  119. {
  120. iterations = int.Parse(hash.Parameters["iterations"]);
  121. }
  122. catch (Exception e)
  123. {
  124. throw new InvalidDataException($"Couldn't successfully parse iterations value from string: {hash.Parameters["iterations"]}", e);
  125. }
  126. }
  127. return PBKDF2(hash.Id, hash.HashBytes, hash.SaltBytes, iterations);
  128. }
  129. public byte[] GenerateSalt()
  130. {
  131. byte[] salt = new byte[64];
  132. rng.GetBytes(salt);
  133. return salt;
  134. }
  135. }
  136. }