2019-01-31 08:24:53 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Cryptography
|
|
|
|
{
|
|
|
|
public class PasswordHash
|
|
|
|
{
|
2019-03-05 07:58:25 +00:00
|
|
|
// Defined from this hash storage spec
|
|
|
|
// https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md
|
2019-03-07 10:41:44 +00:00
|
|
|
// $<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]]
|
|
|
|
// with one slight amendment to ease the transition, we're writing out the bytes in hex
|
2019-03-05 07:58:25 +00:00
|
|
|
// rather than making them a BASE64 string with stripped padding
|
2019-01-31 08:24:53 +00:00
|
|
|
|
2019-03-07 10:41:44 +00:00
|
|
|
private string _id;
|
2019-03-05 07:58:25 +00:00
|
|
|
|
2019-03-07 10:41:44 +00:00
|
|
|
private Dictionary<string, string> _parameters = new Dictionary<string, string>();
|
2019-03-05 07:58:25 +00:00
|
|
|
|
2019-03-07 10:41:44 +00:00
|
|
|
private string _salt;
|
2019-03-05 07:58:25 +00:00
|
|
|
|
2019-03-07 10:41:44 +00:00
|
|
|
private byte[] _saltBytes;
|
2019-03-05 07:58:25 +00:00
|
|
|
|
2019-03-07 10:41:44 +00:00
|
|
|
private string _hash;
|
|
|
|
|
|
|
|
private byte[] _hashBytes;
|
|
|
|
|
|
|
|
public string Id { get => _id; set => _id = value; }
|
|
|
|
|
|
|
|
public Dictionary<string, string> Parameters { get => _parameters; set => _parameters = value; }
|
|
|
|
|
|
|
|
public string Salt { get => _salt; set => _salt = value; }
|
|
|
|
|
|
|
|
public byte[] SaltBytes { get => _saltBytes; set => _saltBytes = value; }
|
|
|
|
|
|
|
|
public string Hash { get => _hash; set => _hash = value; }
|
|
|
|
|
|
|
|
public byte[] HashBytes { get => _hashBytes; set => _hashBytes = value; }
|
2019-03-05 07:58:25 +00:00
|
|
|
|
2019-02-13 08:33:00 +00:00
|
|
|
public PasswordHash(string storageString)
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-02-18 09:26:01 +00:00
|
|
|
string[] splitted = storageString.Split('$');
|
2019-03-05 07:58:25 +00:00
|
|
|
_id = splitted[1];
|
2019-02-18 09:26:01 +00:00
|
|
|
if (splitted[2].Contains("="))
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-02-18 09:26:01 +00:00
|
|
|
foreach (string paramset in (splitted[2].Split(',')))
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-02-20 08:00:26 +00:00
|
|
|
if (!string.IsNullOrEmpty(paramset))
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-03-07 10:41:44 +00:00
|
|
|
string[] fields = paramset.Split('=');
|
|
|
|
if (fields.Length == 2)
|
|
|
|
{
|
|
|
|
_parameters.Add(fields[0], fields[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception($"Malformed parameter in password hash string {paramset}");
|
2019-02-13 08:33:00 +00:00
|
|
|
}
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 09:26:01 +00:00
|
|
|
if (splitted.Length == 5)
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-03-05 07:58:25 +00:00
|
|
|
_salt = splitted[3];
|
|
|
|
_saltBytes = ConvertFromByteString(_salt);
|
|
|
|
_hash = splitted[4];
|
|
|
|
_hashBytes = ConvertFromByteString(_hash);
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-05 07:58:25 +00:00
|
|
|
_salt = string.Empty;
|
|
|
|
_hash = splitted[3];
|
|
|
|
_hashBytes = ConvertFromByteString(_hash);
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-18 09:26:01 +00:00
|
|
|
if (splitted.Length == 4)
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-03-05 07:58:25 +00:00
|
|
|
_salt = splitted[2];
|
|
|
|
_saltBytes = ConvertFromByteString(_salt);
|
|
|
|
_hash = splitted[3];
|
|
|
|
_hashBytes = ConvertFromByteString(_hash);
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-05 07:58:25 +00:00
|
|
|
_salt = string.Empty;
|
|
|
|
_hash = splitted[2];
|
|
|
|
_hashBytes = ConvertFromByteString(_hash);
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-07 10:41:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 08:33:00 +00:00
|
|
|
public PasswordHash(ICryptoProvider cryptoProvider)
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-03-05 07:58:25 +00:00
|
|
|
_id = cryptoProvider.DefaultHashMethod;
|
|
|
|
_saltBytes = cryptoProvider.GenerateSalt();
|
2019-03-07 10:41:44 +00:00
|
|
|
_salt = ConvertToByteString(SaltBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] ConvertFromByteString(string byteString)
|
|
|
|
{
|
|
|
|
List<byte> bytes = new List<byte>();
|
|
|
|
for (int i = 0; i < byteString.Length; i += 2)
|
|
|
|
{
|
|
|
|
// TODO: NetStandard2.1 switch this to use a span instead of a substring.
|
|
|
|
bytes.Add(Convert.ToByte(byteString.Substring(i, 2), 16));
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.ToArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string ConvertToByteString(byte[] bytes)
|
|
|
|
{
|
|
|
|
return BitConverter.ToString(bytes).Replace("-", "");
|
|
|
|
}
|
2019-02-13 08:33:00 +00:00
|
|
|
|
2019-01-31 08:24:53 +00:00
|
|
|
private string SerializeParameters()
|
|
|
|
{
|
2019-02-20 08:00:26 +00:00
|
|
|
string ReturnString = string.Empty;
|
2019-03-05 07:58:25 +00:00
|
|
|
foreach (var KVP in _parameters)
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-02-20 08:00:26 +00:00
|
|
|
ReturnString += $",{KVP.Key}={KVP.Value}";
|
2019-03-07 10:41:44 +00:00
|
|
|
}
|
2019-02-20 08:00:26 +00:00
|
|
|
|
2019-02-12 10:16:03 +00:00
|
|
|
if ((!string.IsNullOrEmpty(ReturnString)) && ReturnString[0] == ',')
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
|
|
|
ReturnString = ReturnString.Remove(0, 1);
|
2019-03-07 10:41:44 +00:00
|
|
|
}
|
2019-02-20 08:00:26 +00:00
|
|
|
|
2019-01-31 08:24:53 +00:00
|
|
|
return ReturnString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
2019-03-07 10:41:44 +00:00
|
|
|
{
|
|
|
|
string outString = "$" + _id;
|
|
|
|
string paramstring = SerializeParameters();
|
|
|
|
if (!string.IsNullOrEmpty(paramstring))
|
|
|
|
{
|
|
|
|
outString += $"${paramstring}";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(_salt))
|
|
|
|
{
|
|
|
|
outString += $"${_salt}";
|
|
|
|
}
|
|
|
|
|
2019-03-05 07:58:25 +00:00
|
|
|
outString += $"${_hash}";
|
2019-02-18 09:26:01 +00:00
|
|
|
return outString;
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 10:41:44 +00:00
|
|
|
}
|