Jackett/src/Jackett.Server/Services/SecurityService.cs

44 lines
1.2 KiB
C#
Raw Normal View History

using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Jackett.Common.Models.Config;
using Jackett.Common.Services.Interfaces;
2018-05-01 11:41:34 +00:00
namespace Jackett.Server.Services
{
2021-11-16 13:06:07 +00:00
internal class SecurityService : ISecurityService
{
private readonly ServerConfig _serverConfig;
2021-11-16 13:06:07 +00:00
public SecurityService(ServerConfig sc) => _serverConfig = sc;
2023-01-07 13:52:15 +00:00
public bool CheckAuthorised(string password)
{
if (string.IsNullOrEmpty(_serverConfig.AdminPassword))
return true;
if (!string.IsNullOrEmpty(password) && HashPassword(password) == _serverConfig.AdminPassword)
return true;
return false;
}
public string HashPassword(string input)
{
if (input == null)
return null;
2023-01-07 13:52:15 +00:00
var ue = new UnicodeEncoding();
2021-11-17 05:06:27 +00:00
#pragma warning disable SYSLIB0021
var hashString = new SHA512Managed();
2021-11-17 05:06:27 +00:00
#pragma warning restore SYSLIB0021
2023-01-07 13:52:15 +00:00
// Append key as salt
input += _serverConfig.APIKey;
var message = ue.GetBytes(input);
var hashValue = hashString.ComputeHash(message);
return hashValue.Aggregate("", (current, x) => current + $"{x:x2}");
}
}
}