mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-25 03:26:32 +00:00
sha256 with salt auth and sha1 interop
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MediaBrowser.Model.Cryptography
|
||||
{
|
||||
public interface ICryptoProvider
|
||||
{
|
||||
Guid GetMD5(string str);
|
||||
byte[] ComputeMD5(Stream str);
|
||||
byte[] ComputeMD5(byte[] bytes);
|
||||
byte[] ComputeSHA1(byte[] bytes);
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MediaBrowser.Model.Cryptography
|
||||
{
|
||||
public interface ICryptoProvider
|
||||
{
|
||||
Guid GetMD5(string str);
|
||||
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);
|
||||
@@ -17,5 +17,6 @@ namespace MediaBrowser.Model.Cryptography
|
||||
byte[] ComputeHashWithDefaultMethod(byte[] bytes, byte[] salt);
|
||||
byte[] ComputeHash(PasswordHash hash);
|
||||
byte[] GenerateSalt();
|
||||
}
|
||||
}
|
||||
string DefaultHashMethod { get; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,15 +33,15 @@ namespace MediaBrowser.Model.Cryptography
|
||||
if (a.Length == 4)
|
||||
{
|
||||
Salt = a[2];
|
||||
SaltBytes = Convert.FromBase64CharArray(Salt.ToCharArray(), 0, Salt.Length);
|
||||
SaltBytes = FromByteString(Salt);
|
||||
Hash = a[3];
|
||||
HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
|
||||
HashBytes = FromByteString(Hash);
|
||||
}
|
||||
else
|
||||
{
|
||||
Salt = string.Empty;
|
||||
Hash = a[3];
|
||||
HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
|
||||
HashBytes = FromByteString(Hash);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -49,15 +49,15 @@ namespace MediaBrowser.Model.Cryptography
|
||||
if (a.Length == 4)
|
||||
{
|
||||
Salt = a[2];
|
||||
SaltBytes = Convert.FromBase64CharArray(Salt.ToCharArray(), 0, Salt.Length);
|
||||
SaltBytes = FromByteString(Salt);
|
||||
Hash = a[3];
|
||||
HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
|
||||
HashBytes = FromByteString(Hash);
|
||||
}
|
||||
else
|
||||
{
|
||||
Salt = string.Empty;
|
||||
Hash = a[2];
|
||||
HashBytes = Convert.FromBase64CharArray(Hash.ToCharArray(), 0, Hash.Length);
|
||||
HashBytes = FromByteString(Hash);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -68,7 +68,17 @@ namespace MediaBrowser.Model.Cryptography
|
||||
{
|
||||
Id = "SHA256";
|
||||
SaltBytes = cryptoProvider2.GenerateSalt();
|
||||
Salt = Convert.ToBase64String(SaltBytes);
|
||||
Salt = BitConverter.ToString(SaltBytes).Replace("-", "");
|
||||
}
|
||||
|
||||
private byte[] FromByteString(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();
|
||||
}
|
||||
private string SerializeParameters()
|
||||
{
|
||||
@@ -77,7 +87,7 @@ namespace MediaBrowser.Model.Cryptography
|
||||
{
|
||||
ReturnString += String.Format(",{0}={1}", KVP.Key, KVP.Value);
|
||||
}
|
||||
if (ReturnString[0] == ',')
|
||||
if ((!string.IsNullOrEmpty(ReturnString)) && ReturnString[0] == ',')
|
||||
{
|
||||
ReturnString = ReturnString.Remove(0, 1);
|
||||
}
|
||||
@@ -85,8 +95,15 @@ namespace MediaBrowser.Model.Cryptography
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("${0}${1}${2}${3}", Id, SerializeParameters(), Salt, Hash);
|
||||
{
|
||||
string OutString = "$";
|
||||
OutString += Id;
|
||||
if (!string.IsNullOrEmpty(SerializeParameters()))
|
||||
OutString += $"${SerializeParameters()}";
|
||||
if (!string.IsNullOrEmpty(Salt))
|
||||
OutString += $"${Salt}";
|
||||
OutString += $"${Hash}";
|
||||
return OutString;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user