CryptoExtensions.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Text;
  4. using MediaBrowser.Model.Cryptography;
  5. using static MediaBrowser.Common.Cryptography.Constants;
  6. namespace MediaBrowser.Common.Cryptography
  7. {
  8. /// <summary>
  9. /// Class containing extension methods for working with Jellyfin cryptography objects.
  10. /// </summary>
  11. public static class CryptoExtensions
  12. {
  13. /// <summary>
  14. /// Creates a new <see cref="PasswordHash" /> instance.
  15. /// </summary>
  16. /// <param name="cryptoProvider">The <see cref="ICryptoProvider" /> instance used.</param>
  17. /// <param name="password">The password that will be hashed.</param>
  18. /// <returns>A <see cref="PasswordHash" /> instance with the hash method, hash, salt and number of iterations.</returns>
  19. public static PasswordHash CreatePasswordHash(this ICryptoProvider cryptoProvider, string password)
  20. {
  21. byte[] salt = cryptoProvider.GenerateSalt();
  22. return new PasswordHash(
  23. cryptoProvider.DefaultHashMethod,
  24. cryptoProvider.ComputeHashWithDefaultMethod(
  25. Encoding.UTF8.GetBytes(password),
  26. salt),
  27. salt,
  28. new Dictionary<string, string>
  29. {
  30. { "iterations", DefaultIterations.ToString(CultureInfo.InvariantCulture) }
  31. });
  32. }
  33. }
  34. }