aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Cryptography
diff options
context:
space:
mode:
authorPhallacy <Dragoonmac@gmail.com>2019-01-31 00:24:53 -0800
committerPhallacy <Dragoonmac@gmail.com>2019-01-31 00:24:53 -0800
commit4519ce26e2250cb233836296d292ddb7b3cf6346 (patch)
tree6d3781ac47a3a76406750a90f4a76d87efeba007 /MediaBrowser.Model/Cryptography
parent49d9649b8eb7e9edc889c10a679e980486ee0018 (diff)
Upgrade crypto provider, retarget better framework
Diffstat (limited to 'MediaBrowser.Model/Cryptography')
-rw-r--r--MediaBrowser.Model/Cryptography/ICryptoProvider.cs8
-rw-r--r--MediaBrowser.Model/Cryptography/PasswordHash.cs93
2 files changed, 101 insertions, 0 deletions
diff --git a/MediaBrowser.Model/Cryptography/ICryptoProvider.cs b/MediaBrowser.Model/Cryptography/ICryptoProvider.cs
index b027d2ad0..ec7e57fec 100644
--- a/MediaBrowser.Model/Cryptography/ICryptoProvider.cs
+++ b/MediaBrowser.Model/Cryptography/ICryptoProvider.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Collections.Generic;
namespace MediaBrowser.Model.Cryptography
{
@@ -9,5 +10,12 @@ namespace MediaBrowser.Model.Cryptography
byte[] ComputeMD5(Stream str);
byte[] ComputeMD5(byte[] bytes);
byte[] ComputeSHA1(byte[] bytes);
+ IEnumerable<string> GetSupportedHashMethods();
+ byte[] ComputeHash(string HashMethod, byte[] bytes);
+ byte[] ComputeHashWithDefaultMethod(byte[] bytes);
+ byte[] ComputeHash(string HashMethod, byte[] bytes, byte[] salt);
+ byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt);
+ byte[] ComputeHash(PasswordHash hash);
+ byte[] GenerateSalt();
}
}
diff --git a/MediaBrowser.Model/Cryptography/PasswordHash.cs b/MediaBrowser.Model/Cryptography/PasswordHash.cs
new file mode 100644
index 000000000..d37220ab2
--- /dev/null
+++ b/MediaBrowser.Model/Cryptography/PasswordHash.cs
@@ -0,0 +1,93 @@
+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[] a = StorageString.Split('$');
+ Id = a[1];
+ if (a[2].Contains("="))
+ {
+ foreach (string paramset in (a[2].Split(',')))
+ {
+ if (!String.IsNullOrEmpty(paramset))
+ {
+ string[] fields = paramset.Split('=');
+ Parameters.Add(fields[0], fields[1]);
+ }
+ }
+ if (a.Length == 4)
+ {
+ Salt = a[2];
+ SaltBytes = Convert.FromBase64CharArray(Salt.ToCharArray(), 0, Salt.Length);
+ Hash = a[3];
+ HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
+ }
+ else
+ {
+ Salt = string.Empty;
+ Hash = a[3];
+ HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
+ }
+ }
+ else
+ {
+ if (a.Length == 4)
+ {
+ Salt = a[2];
+ SaltBytes = Convert.FromBase64CharArray(Salt.ToCharArray(), 0, Salt.Length);
+ Hash = a[3];
+ HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
+ }
+ else
+ {
+ Salt = string.Empty;
+ Hash = a[2];
+ HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
+ }
+
+ }
+
+ }
+
+ public PasswordHash(ICryptoProvider cryptoProvider2)
+ {
+ Id = "SHA256";
+ SaltBytes = cryptoProvider2.GenerateSalt();
+ Salt = Convert.ToBase64String(SaltBytes);
+ }
+ private string SerializeParameters()
+ {
+ string ReturnString = String.Empty;
+ foreach (var KVP in Parameters)
+ {
+ ReturnString += String.Format(",{0}={1}", KVP.Key, KVP.Value);
+ }
+ if (ReturnString[0] == ',')
+ {
+ ReturnString = ReturnString.Remove(0, 1);
+ }
+ return ReturnString;
+ }
+
+ public override string ToString()
+ {
+ return String.Format("${0}${1}${2}${3}", Id, SerializeParameters(), Salt, Hash);
+ }
+ }
+
+}