You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.1 KiB
62 lines
2.1 KiB
using Infrastructure.Extensions;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Infrastructure.Security
|
|
{
|
|
public class EncryptionService : IEncryptionService
|
|
{
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public EncryptionService(IConfiguration configuration)
|
|
{
|
|
this._configuration = configuration;
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5350:不要使用弱加密算法", Justification = "<挂起>")]
|
|
public string CreatePasswordHash(string password, string saltkey)
|
|
{
|
|
var saltAndPassword = String.Concat(password, saltkey);
|
|
using var algorithm = SHA1.Create();
|
|
var bytes = algorithm.ComputeHash(Encoding.UTF8.GetBytes(saltAndPassword));
|
|
var result = string.Empty;
|
|
var builder = new StringBuilder();
|
|
builder.Append(result);
|
|
foreach (var item in bytes)
|
|
{
|
|
builder.Append($"{item:x2}");
|
|
}
|
|
result = builder.ToString();
|
|
return result;
|
|
}
|
|
|
|
public string CreateSalt()
|
|
{
|
|
var code = string.Empty;
|
|
var builder = new StringBuilder();
|
|
builder.Append(code);
|
|
for (int i = 0; i < 6; i++)
|
|
{
|
|
var random = new byte[1];
|
|
using var rg = RandomNumberGenerator.Create();
|
|
rg.GetBytes(random);
|
|
builder.Append(new Random(Convert.ToInt32(random[0])).Next(0, 9));
|
|
}
|
|
code = builder.ToString();
|
|
return code;
|
|
}
|
|
|
|
public string EncryptObject(object obj)
|
|
{
|
|
return JsonConvert.SerializeObject(obj).DESEncrypt(this._configuration.GetSection("security").GetValue<string>("key"));
|
|
}
|
|
|
|
public T DecryptObject<T>(string value)
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(value.DESDecrypt(this._configuration.GetSection("security").GetValue<string>("key")));
|
|
}
|
|
}
|
|
} |