feat: 初始功能
This commit is contained in:
34
Services/DpapiSecretProtector.cs
Normal file
34
Services/DpapiSecretProtector.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using CloudPrint.Client.Services.Abstractions;
|
||||
|
||||
namespace CloudPrint.Client.Services;
|
||||
|
||||
public sealed class DpapiSecretProtector : ISecretProtector
|
||||
{
|
||||
private static readonly byte[] Entropy = Encoding.UTF8.GetBytes("CloudPrint.Client.Settings.v1");
|
||||
|
||||
public string Protect(string plainText)
|
||||
{
|
||||
if (string.IsNullOrEmpty(plainText))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(plainText);
|
||||
var protectedBytes = ProtectedData.Protect(bytes, Entropy, DataProtectionScope.CurrentUser);
|
||||
return Convert.ToBase64String(protectedBytes);
|
||||
}
|
||||
|
||||
public string Unprotect(string protectedText)
|
||||
{
|
||||
if (string.IsNullOrEmpty(protectedText))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var protectedBytes = Convert.FromBase64String(protectedText);
|
||||
var bytes = ProtectedData.Unprotect(protectedBytes, Entropy, DataProtectionScope.CurrentUser);
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user