2019-01-31 08:24:53 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Cryptography
|
|
|
|
{
|
|
|
|
public class PasswordHash
|
|
|
|
{
|
|
|
|
//Defined from this hash storage spec
|
|
|
|
//https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md
|
|
|
|
//$<id>[$<param>=<value>(,<param>=<value>)*][$<salt>[$<hash>]]
|
|
|
|
|
|
|
|
public string Id;
|
|
|
|
public Dictionary<string, string> Parameters = new Dictionary<string, string>();
|
|
|
|
public string Salt;
|
|
|
|
public byte[] SaltBytes;
|
|
|
|
public string Hash;
|
|
|
|
public byte[] HashBytes;
|
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('$');
|
|
|
|
Id = splitted[1];
|
|
|
|
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
|
|
|
{
|
|
|
|
if (!String.IsNullOrEmpty(paramset))
|
|
|
|
{
|
2019-02-13 08:33:00 +00:00
|
|
|
string[] fields = paramset.Split('=');
|
2019-02-18 09:26:01 +00:00
|
|
|
if (fields.Length == 2)
|
2019-02-13 08:33:00 +00:00
|
|
|
{
|
|
|
|
Parameters.Add(fields[0], fields[1]);
|
2019-02-18 09:26:01 +00:00
|
|
|
}
|
|
|
|
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-02-18 09:26:01 +00:00
|
|
|
Salt = splitted[3];
|
2019-02-13 08:33:00 +00:00
|
|
|
SaltBytes = ConvertFromByteString(Salt);
|
2019-02-18 09:26:01 +00:00
|
|
|
Hash = splitted[4];
|
2019-02-13 08:33:00 +00:00
|
|
|
HashBytes = ConvertFromByteString(Hash);
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Salt = string.Empty;
|
2019-02-18 09:26:01 +00:00
|
|
|
Hash = splitted[3];
|
2019-02-13 08:33:00 +00:00
|
|
|
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-02-18 09:26:01 +00:00
|
|
|
Salt = splitted[2];
|
2019-02-13 08:33:00 +00:00
|
|
|
SaltBytes = ConvertFromByteString(Salt);
|
2019-02-18 09:26:01 +00:00
|
|
|
Hash = splitted[3];
|
2019-02-13 08:33:00 +00:00
|
|
|
HashBytes = ConvertFromByteString(Hash);
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Salt = string.Empty;
|
2019-02-18 09:26:01 +00:00
|
|
|
Hash = splitted[2];
|
2019-02-13 08:33:00 +00:00
|
|
|
HashBytes = ConvertFromByteString(Hash);
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-02-13 08:33:00 +00:00
|
|
|
public PasswordHash(ICryptoProvider cryptoProvider)
|
2019-01-31 08:24:53 +00:00
|
|
|
{
|
2019-02-13 08:33:00 +00:00
|
|
|
Id = cryptoProvider.DefaultHashMethod;
|
|
|
|
SaltBytes = cryptoProvider.GenerateSalt();
|
|
|
|
Salt = ConvertToByteString(SaltBytes);
|
2019-02-12 10:16:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 08:33:00 +00:00
|
|
|
public static byte[] ConvertFromByteString(string byteString)
|
2019-02-12 10:16:03 +00:00
|
|
|
{
|
|
|
|
List<byte> Bytes = new List<byte>();
|
2019-02-13 08:33:00 +00:00
|
|
|
for (int i = 0; i < byteString.Length; i += 2)
|
2019-02-12 10:16:03 +00:00
|
|
|
{
|
2019-02-13 08:33:00 +00:00
|
|
|
Bytes.Add(Convert.ToByte(byteString.Substring(i, 2),16));
|
2019-02-12 10:16:03 +00:00
|
|
|
}
|
|
|
|
return Bytes.ToArray();
|
2019-02-13 08:33:00 +00:00
|
|
|
}
|
2019-02-18 09:26:01 +00:00
|
|
|
|
2019-02-13 08:33:00 +00:00
|
|
|
public static string ConvertToByteString(byte[] bytes)
|
|
|
|
{
|
|
|
|
return BitConverter.ToString(bytes).Replace("-", "");
|
|
|
|
}
|
|
|
|
|
2019-01-31 08:24:53 +00:00
|
|
|
private string SerializeParameters()
|
|
|
|
{
|
|
|
|
string ReturnString = String.Empty;
|
|
|
|
foreach (var KVP in Parameters)
|
|
|
|
{
|
|
|
|
ReturnString += String.Format(",{0}={1}", KVP.Key, KVP.Value);
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
return ReturnString;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string ToString()
|
2019-02-12 10:16:03 +00:00
|
|
|
{
|
2019-02-18 09:26:01 +00:00
|
|
|
string outString = "$" +Id;
|
2019-02-13 08:33:00 +00:00
|
|
|
string paramstring = SerializeParameters();
|
|
|
|
if (!string.IsNullOrEmpty(paramstring))
|
|
|
|
{
|
2019-02-18 09:26:01 +00:00
|
|
|
outString += $"${paramstring}";
|
2019-02-13 08:33:00 +00:00
|
|
|
}
|
2019-02-12 10:16:03 +00:00
|
|
|
if (!string.IsNullOrEmpty(Salt))
|
2019-02-13 08:33:00 +00:00
|
|
|
{
|
2019-02-18 09:26:01 +00:00
|
|
|
outString += $"${Salt}";
|
2019-02-13 08:33:00 +00:00
|
|
|
}
|
2019-02-18 09:26:01 +00:00
|
|
|
outString += $"${Hash}";
|
|
|
|
return outString;
|
2019-01-31 08:24:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|