aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Cryptography/PasswordHash.cs
blob: cd61657c182582a5b84f4cb51e7cfaf09e8ac5e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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;
        public PasswordHash(string storageString)
        {
            string[] SplitStorageString = storageString.Split('$');
            Id = SplitStorageString[1];
            if (SplitStorageString[2].Contains("="))
            {
                foreach (string paramset in (SplitStorageString[2].Split(',')))
                {
                    if (!String.IsNullOrEmpty(paramset))
                    {
                        string[] fields = paramset.Split('=');
                        if(fields.Length == 2)
                        {
                            Parameters.Add(fields[0], fields[1]);
                        }
                    }
                }
                if (SplitStorageString.Length == 5)
                {
                    Salt = SplitStorageString[3];
                    SaltBytes = ConvertFromByteString(Salt);
                    Hash = SplitStorageString[4];
                    HashBytes = ConvertFromByteString(Hash);
                }
                else
                {
                    Salt = string.Empty;
                    Hash = SplitStorageString[3];
                    HashBytes = ConvertFromByteString(Hash);
                }
            }
            else
            {
                if (SplitStorageString.Length == 4)
                {
                    Salt = SplitStorageString[2];
                    SaltBytes = ConvertFromByteString(Salt);
                    Hash = SplitStorageString[3];
                    HashBytes = ConvertFromByteString(Hash);
                }
                else
                {
                    Salt = string.Empty;
                    Hash = SplitStorageString[2];
                    HashBytes = ConvertFromByteString(Hash);
                }

            }

        }

        public PasswordHash(ICryptoProvider cryptoProvider)
        {
            Id = cryptoProvider.DefaultHashMethod;
            SaltBytes = cryptoProvider.GenerateSalt();
            Salt = ConvertToByteString(SaltBytes);
        }

        public static byte[] ConvertFromByteString(string byteString)
        {
            List<byte> Bytes = new List<byte>();
            for (int i = 0; i < byteString.Length; i += 2)
            {
                Bytes.Add(Convert.ToByte(byteString.Substring(i, 2),16));
            }
            return Bytes.ToArray();
        }
        public static string ConvertToByteString(byte[] bytes)
        {
            return BitConverter.ToString(bytes).Replace("-", "");
        }

        private string SerializeParameters()
        {
            string ReturnString = String.Empty;
            foreach (var KVP in Parameters)
            {
                ReturnString += String.Format(",{0}={1}", KVP.Key, KVP.Value);
            }
            if ((!string.IsNullOrEmpty(ReturnString)) && ReturnString[0] == ',')
            {
                ReturnString = ReturnString.Remove(0, 1);
            }
            return ReturnString;
        }

        public override string ToString()
        {
            string OutString = "$";
            OutString += Id;
            string paramstring = SerializeParameters();
            if (!string.IsNullOrEmpty(paramstring))
            {
                OutString += $"${paramstring}";
            }
            if (!string.IsNullOrEmpty(Salt))
            {
                OutString += $"${Salt}";
            }
            OutString += $"${Hash}";
            return OutString;
        }
    }

}