PasswordHash.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using static MediaBrowser.Common.HexHelper;
  6. namespace MediaBrowser.Common.Cryptography
  7. {
  8. // Defined from this hash storage spec
  9. // https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md
  10. // $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]]
  11. // with one slight amendment to ease the transition, we're writing out the bytes in hex
  12. // rather than making them a BASE64 string with stripped padding
  13. public class PasswordHash
  14. {
  15. private readonly Dictionary<string, string> _parameters;
  16. public PasswordHash(string id, byte[] hash)
  17. : this(id, hash, Array.Empty<byte>())
  18. {
  19. }
  20. public PasswordHash(string id, byte[] hash, byte[] salt)
  21. : this(id, hash, salt, new Dictionary<string, string>())
  22. {
  23. }
  24. public PasswordHash(string id, byte[] hash, byte[] salt, Dictionary<string, string> parameters)
  25. {
  26. Id = id;
  27. Hash = hash;
  28. Salt = salt;
  29. _parameters = parameters;
  30. }
  31. /// <summary>
  32. /// Gets the symbolic name for the function used.
  33. /// </summary>
  34. /// <value>Returns the symbolic name for the function used.</value>
  35. public string Id { get; }
  36. /// <summary>
  37. /// Gets the additional parameters used by the hash function.
  38. /// </summary>
  39. /// <value></value>
  40. public IReadOnlyDictionary<string, string> Parameters => _parameters;
  41. /// <summary>
  42. /// Gets the salt used for hashing the password.
  43. /// </summary>
  44. /// <value>Returns the salt used for hashing the password.</value>
  45. public byte[] Salt { get; }
  46. /// <summary>
  47. /// Gets the hashed password.
  48. /// </summary>
  49. /// <value>Return the hashed password.</value>
  50. public byte[] Hash { get; }
  51. public static PasswordHash Parse(string storageString)
  52. {
  53. string[] splitted = storageString.Split('$');
  54. // The string should at least contain the hash function and the hash itself
  55. if (splitted.Length < 3)
  56. {
  57. throw new ArgumentException("String doesn't contain enough segments", nameof(storageString));
  58. }
  59. // Start at 1, the first index shouldn't contain any data
  60. int index = 1;
  61. // Name of the hash function
  62. string id = splitted[index++];
  63. // Optional parameters
  64. Dictionary<string, string> parameters = new Dictionary<string, string>();
  65. if (splitted[index].IndexOf('=') != -1)
  66. {
  67. foreach (string paramset in splitted[index++].Split(','))
  68. {
  69. if (string.IsNullOrEmpty(paramset))
  70. {
  71. continue;
  72. }
  73. string[] fields = paramset.Split('=');
  74. if (fields.Length != 2)
  75. {
  76. throw new InvalidDataException($"Malformed parameter in password hash string {paramset}");
  77. }
  78. parameters.Add(fields[0], fields[1]);
  79. }
  80. }
  81. byte[] hash;
  82. byte[] salt;
  83. // Check if the string also contains a salt
  84. if (splitted.Length - index == 2)
  85. {
  86. salt = FromHexString(splitted[index++]);
  87. hash = FromHexString(splitted[index++]);
  88. }
  89. else
  90. {
  91. salt = Array.Empty<byte>();
  92. hash = FromHexString(splitted[index++]);
  93. }
  94. return new PasswordHash(id, hash, salt, parameters);
  95. }
  96. private void SerializeParameters(StringBuilder stringBuilder)
  97. {
  98. if (_parameters.Count == 0)
  99. {
  100. return;
  101. }
  102. stringBuilder.Append('$');
  103. foreach (var pair in _parameters)
  104. {
  105. stringBuilder.Append(pair.Key);
  106. stringBuilder.Append('=');
  107. stringBuilder.Append(pair.Value);
  108. stringBuilder.Append(',');
  109. }
  110. // Remove last ','
  111. stringBuilder.Length -= 1;
  112. }
  113. /// <inheritdoc />
  114. public override string ToString()
  115. {
  116. var str = new StringBuilder();
  117. str.Append('$');
  118. str.Append(Id);
  119. SerializeParameters(str);
  120. if (Salt.Length != 0)
  121. {
  122. str.Append('$');
  123. str.Append(ToHexString(Salt));
  124. }
  125. str.Append('$');
  126. str.Append(ToHexString(Hash));
  127. return str.ToString();
  128. }
  129. }
  130. }