PasswordHash.cs 4.4 KB

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