mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
Upgrade crypto provider, retarget better framework
This commit is contained in:
parent
49d9649b8e
commit
4519ce26e2
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -8,6 +9,34 @@ namespace Emby.Server.Implementations.Cryptography
|
|||||||
{
|
{
|
||||||
public class CryptographyProvider : ICryptoProvider
|
public class CryptographyProvider : ICryptoProvider
|
||||||
{
|
{
|
||||||
|
private List<string> SupportedHashMethods = new List<string>();
|
||||||
|
private string DefaultHashMethod = "SHA256";
|
||||||
|
private RandomNumberGenerator rng;
|
||||||
|
private int defaultiterations = 1000;
|
||||||
|
public CryptographyProvider()
|
||||||
|
{
|
||||||
|
//Currently supported hash methods from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=netcore-2.1
|
||||||
|
//there might be a better way to autogenerate this list as dotnet updates, but I couldn't find one
|
||||||
|
SupportedHashMethods = new List<string>
|
||||||
|
{
|
||||||
|
"MD5"
|
||||||
|
,"System.Security.Cryptography.MD5"
|
||||||
|
,"SHA"
|
||||||
|
,"SHA1"
|
||||||
|
,"System.Security.Cryptography.SHA1"
|
||||||
|
,"SHA256"
|
||||||
|
,"SHA-256"
|
||||||
|
,"System.Security.Cryptography.SHA256"
|
||||||
|
,"SHA384"
|
||||||
|
,"SHA-384"
|
||||||
|
,"System.Security.Cryptography.SHA384"
|
||||||
|
,"SHA512"
|
||||||
|
,"SHA-512"
|
||||||
|
,"System.Security.Cryptography.SHA512"
|
||||||
|
};
|
||||||
|
rng = RandomNumberGenerator.Create();
|
||||||
|
}
|
||||||
|
|
||||||
public Guid GetMD5(string str)
|
public Guid GetMD5(string str)
|
||||||
{
|
{
|
||||||
return new Guid(ComputeMD5(Encoding.Unicode.GetBytes(str)));
|
return new Guid(ComputeMD5(Encoding.Unicode.GetBytes(str)));
|
||||||
@ -36,5 +65,67 @@ namespace Emby.Server.Implementations.Cryptography
|
|||||||
return provider.ComputeHash(bytes);
|
return provider.ComputeHash(bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<string> GetSupportedHashMethods()
|
||||||
|
{
|
||||||
|
return SupportedHashMethods;
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] PBKDF2(string method, byte[] bytes, byte[] salt)
|
||||||
|
{
|
||||||
|
using (var r = new Rfc2898DeriveBytes(bytes, salt, defaultiterations, new HashAlgorithmName(method)))
|
||||||
|
{
|
||||||
|
return r.GetBytes(32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] ComputeHash(string HashMethod, byte[] bytes)
|
||||||
|
{
|
||||||
|
return ComputeHash(HashMethod, bytes, new byte[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] ComputeHashWithDefaultMethod(byte[] bytes)
|
||||||
|
{
|
||||||
|
return ComputeHash(DefaultHashMethod, bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] ComputeHash(string HashMethod, byte[] bytes, byte[] salt)
|
||||||
|
{
|
||||||
|
if (SupportedHashMethods.Contains(HashMethod))
|
||||||
|
{
|
||||||
|
if (salt.Length == 0)
|
||||||
|
{
|
||||||
|
using (var h = HashAlgorithm.Create(HashMethod))
|
||||||
|
{
|
||||||
|
return h.ComputeHash(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return PBKDF2(HashMethod, bytes, salt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new CryptographicException(String.Format("Requested hash method is not supported: {0}", HashMethod));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt)
|
||||||
|
{
|
||||||
|
return PBKDF2(DefaultHashMethod, bytes, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] ComputeHash(PasswordHash hash)
|
||||||
|
{
|
||||||
|
return ComputeHash(hash.Id, hash.HashBytes, hash.SaltBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] GenerateSalt()
|
||||||
|
{
|
||||||
|
byte[] salt = new byte[8];
|
||||||
|
rng.GetBytes(salt);
|
||||||
|
return salt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ using System.Globalization;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Common.Events;
|
using MediaBrowser.Common.Events;
|
||||||
@ -222,20 +223,18 @@ namespace Emby.Server.Implementations.Library
|
|||||||
|
|
||||||
public bool IsValidUsername(string username)
|
public bool IsValidUsername(string username)
|
||||||
{
|
{
|
||||||
// Usernames can contain letters (a-z), numbers (0-9), dashes (-), underscores (_), apostrophes ('), and periods (.)
|
//The old way was dumb, we should make it less dumb, lets do so.
|
||||||
foreach (var currentChar in username)
|
//This is some regex that matches only on unicode "word" characters, as well as -, _ and @
|
||||||
{
|
//In theory this will cut out most if not all 'control' characters which should help minimize any weirdness
|
||||||
if (!IsValidUsernameCharacter(currentChar))
|
string UserNameRegex = "^[\\w-'._@]*$";
|
||||||
{
|
// Usernames can contain letters (a-z + whatever else unicode is cool with), numbers (0-9), dashes (-), underscores (_), apostrophes ('), and periods (.)
|
||||||
return false;
|
return Regex.IsMatch(username, UserNameRegex);
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsValidUsernameCharacter(char i)
|
private static bool IsValidUsernameCharacter(char i)
|
||||||
{
|
{
|
||||||
return !char.Equals(i, '<') && !char.Equals(i, '>');
|
string UserNameRegex = "^[\\w-'._@]*$";
|
||||||
|
return Regex.IsMatch(i.ToString(), UserNameRegex);
|
||||||
}
|
}
|
||||||
|
|
||||||
public string MakeValidUsername(string username)
|
public string MakeValidUsername(string username)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace MediaBrowser.Model.Cryptography
|
namespace MediaBrowser.Model.Cryptography
|
||||||
{
|
{
|
||||||
@ -9,5 +10,12 @@ namespace MediaBrowser.Model.Cryptography
|
|||||||
byte[] ComputeMD5(Stream str);
|
byte[] ComputeMD5(Stream str);
|
||||||
byte[] ComputeMD5(byte[] bytes);
|
byte[] ComputeMD5(byte[] bytes);
|
||||||
byte[] ComputeSHA1(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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
93
MediaBrowser.Model/Cryptography/PasswordHash.cs
Normal file
93
MediaBrowser.Model/Cryptography/PasswordHash.cs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user