Files
client/Services/DpapiSecretProtector.cs

34 lines
1.0 KiB
C#
Raw Normal View History

2026-05-23 18:16:50 +08:00
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);
}
}