security fix

This commit is contained in:
Aigio Liu
2021-03-08 17:10:26 +08:00
parent 77eff6522f
commit ffc0401409
21 changed files with 383 additions and 245 deletions
@@ -0,0 +1,30 @@
using System.Threading.Tasks;
namespace System.Application.Services
{
/// <summary>
/// 安全服务
/// </summary>
public interface ISecurityService
{
/// <summary>
/// 加密
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
Task<byte[]?> E(string? value);
/// <inheritdoc cref="E(string?)"/>
Task<byte[]?> EB(byte[]? value);
/// <summary>
/// 解密
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
Task<string?> D(byte[]? value);
/// <inheritdoc cref="D(byte[]?)"/>
Task<byte[]?> DB(byte[]? value);
}
}
@@ -0,0 +1,89 @@
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Text;
using System.Diagnostics.CodeAnalysis;
namespace System.Application.Services.Implementation
{
/// <inheritdoc cref="ISecurityService"/>
public abstract class SecurityServiceBase : ISecurityService
{
public abstract Aes? Aes { get; }
public byte[]? E_(string? value)
{
var aes = Aes;
if (aes != null)
{
return AESUtils.EncryptToByteArray_Nullable(aes, value);
}
else
{
if (value == null) return null;
return Encoding.UTF8.GetBytes(value);
}
}
public byte[]? EB_(byte[]? value)
{
var aes = Aes;
if (aes != null)
{
return AESUtils.Encrypt_Nullable(aes, value);
}
else
{
return value;
}
}
public string? D_(byte[]? value)
{
var aes = Aes;
if (aes != null)
{
try
{
return AESUtils.DecryptToString_Nullable(aes, value);
}
catch
{
return null;
}
}
else
{
if (value == null) return null;
return Encoding.UTF8.GetString(value);
}
}
public byte[]? DB_(byte[]? value)
{
var aes = Aes;
if (aes != null)
{
try
{
return AESUtils.Decrypt_Nullable(aes, value);
}
catch
{
return null;
}
}
else
{
return value;
}
}
public virtual Task<byte[]?> E(string? value) => Task.FromResult(E_(value));
public virtual Task<byte[]?> EB(byte[]? value) => Task.FromResult(EB_(value));
public virtual Task<string?> D(byte[]? value) => Task.FromResult(D_(value));
public virtual Task<byte[]?> DB(byte[]? value) => Task.FromResult(DB_(value));
}
}
@@ -1,4 +1,6 @@
using System.Application.Repositories;
using System.Application.Services;
using System.Security.Cryptography;
using System.Threading.Tasks;
using KeyValuePair = System.Application.Entities.KeyValuePair;
@@ -6,26 +8,39 @@ namespace System.Security
{
internal sealed class DesktopClientStorage : Repository<KeyValuePair, string>, IStorage
{
readonly ISecurityService ss;
public DesktopClientStorage(ISecurityService ss)
{
this.ss = ss;
}
bool IStorage.IsNativeSupportedBytes => true;
static string GetKey(string key) => Hashs.String.SHA256(key);
async Task<byte[]?> IStorage.GetBytesAsync(string key)
{
key = GetKey(key);
var item = await FirstOrDefaultAsync(x => x.Id == key);
var value = item?.Value;
return value;
var value2 = await ss.DB(value);
return value2;
}
async Task InsertOrUpdateAsync(string key, byte[] value)
{
var value2 = await ss.EB(value);
await InsertOrUpdateAsync(new KeyValuePair
{
Id = key,
Value = value,
Value = value2.ThrowIsNull(nameof(value2)),
});
}
Task IStorage.SetAsync(string key, byte[]? value)
{
key = GetKey(key);
if (value == null || value.Length <= 0)
{
return DeleteAsync(key);
@@ -38,6 +53,7 @@ namespace System.Security
async Task<bool> IStorage.RemoveAsync(string key)
{
key = GetKey(key);
var result = await DeleteAsync(key);
return result > 0;
}