feat: SimpleEncryptionHelper 支持默认字符串

This commit is contained in:
uye
2025-02-17 12:57:10 +08:00
parent 190f187ca3
commit bd74b82fd6
2 changed files with 29 additions and 3 deletions

View File

@@ -10,13 +10,26 @@ public static class SimpleEncryptionHelper
{
private static readonly ILogger _logger = Log.ForContext("SourceContext", "SimpleEncryptionHelper");
public static string Encrypt(string plainText, DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "")
/// <summary>
/// 使用数据保护API对明文进行加密。
/// </summary>
/// <param name="plainText">需要加密的文本。如果为null或空字符串则返回空字符串。</param>
/// <param name="defaultText">用于比较的默认文本。如果plainText与defaultText相同则直接返回plainText而不加密。</param>
/// <param name="dataProtectionScope">数据保护的范围默认为CurrentUser当前用户。</param>
/// <param name="caller">调用方法的名称,由编译器自动填充。</param>
/// <returns>返回加密后的Base64编码字符串。如果加密失败则返回原始plainText。</returns>
public static string Encrypt(string plainText, string defaultText = "", DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "")
{
if (string.IsNullOrEmpty(plainText))
{
return string.Empty;
}
if (plainText == defaultText)
{
return plainText;
}
try
{
var data = Encoding.UTF8.GetBytes(plainText);
@@ -31,13 +44,26 @@ public static class SimpleEncryptionHelper
}
}
public static string Decrypt(string encryptedText, DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "")
/// <summary>
/// 使用数据保护API对加密文本进行解密。
/// </summary>
/// <param name="encryptedText">需要解密的Base64编码加密文本。如果为null或空字符串则返回空字符串。</param>
/// <param name="defaultText">用于比较的默认文本。如果encryptedText与defaultText相同则直接返回defaultText而不解密。</param>
/// <param name="dataProtectionScope">数据保护的范围默认为CurrentUser当前用户。</param>
/// <param name="caller">调用方法的名称,由编译器自动填充。</param>
/// <returns>返回解密后的文本。如果解密失败则返回原始encryptedText。</returns>
public static string Decrypt(string encryptedText, string defaultText = "", DataProtectionScope dataProtectionScope = DataProtectionScope.CurrentUser, [CallerMemberName] string caller = "")
{
if (string.IsNullOrEmpty(encryptedText))
{
return string.Empty;
}
if (encryptedText == defaultText)
{
return defaultText;
}
try
{
var data = Convert.FromBase64String(encryptedText);

View File

@@ -187,7 +187,7 @@ public class ExternalNotificationSettingsUserControlModel : PropertyChangedBase
}
}
private string _barkServer = SimpleEncryptionHelper.Decrypt(ConfigurationHelper.GetValue(ConfigurationKeys.ExternalNotificationBarkServer, "https://api.day.app"));
private string _barkServer = SimpleEncryptionHelper.Decrypt(ConfigurationHelper.GetValue(ConfigurationKeys.ExternalNotificationBarkServer, "https://api.day.app"), "https://api.day.app");
public string BarkServer
{