PalacePal/Pal.Client/Configuration/ConfigurationData.cs

44 lines
1.4 KiB
C#
Raw Normal View History

using System;
2023-02-15 12:27:41 +00:00
using System.Linq;
using System.Security.Cryptography;
using Microsoft.Extensions.Logging;
2023-02-15 12:00:00 +00:00
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Configuration;
internal static class ConfigurationData
2023-02-15 01:38:04 +00:00
{
2023-03-30 20:01:43 +00:00
private static readonly ILogger _logger =
DependencyInjectionContext.LoggerProvider.CreateLogger(typeof(ConfigurationData));
2023-03-30 20:01:43 +00:00
[Obsolete("for V1 import")]
internal static readonly byte[] FixedV1Entropy = { 0x22, 0x4b, 0xe7, 0x21, 0x44, 0x83, 0x69, 0x55, 0x80, 0x38 };
2023-02-15 12:27:41 +00:00
2023-03-30 20:01:43 +00:00
public const string ConfigFileName = "palace-pal.config.json";
2023-03-30 20:01:43 +00:00
private static bool? _supportsDpapi;
public static bool SupportsDpapi
{
get
2023-02-15 12:27:41 +00:00
{
2023-03-30 20:01:43 +00:00
if (_supportsDpapi == null)
2023-02-15 12:27:41 +00:00
{
2023-03-30 20:01:43 +00:00
try
2023-02-15 12:27:41 +00:00
{
2023-03-30 20:01:43 +00:00
byte[] input = RandomNumberGenerator.GetBytes(32);
byte[] entropy = RandomNumberGenerator.GetBytes(16);
byte[] temp = ProtectedData.Protect(input, entropy, DataProtectionScope.CurrentUser);
byte[] output = ProtectedData.Unprotect(temp, entropy, DataProtectionScope.CurrentUser);
_supportsDpapi = input.SequenceEqual(output);
2023-02-15 12:27:41 +00:00
}
2023-03-30 20:01:43 +00:00
catch (Exception)
{
_supportsDpapi = false;
}
_logger.LogTrace("DPAPI support: {Supported}", _supportsDpapi);
2023-02-15 12:27:41 +00:00
}
2023-03-30 20:01:43 +00:00
return _supportsDpapi.Value;
2023-02-15 12:27:41 +00:00
}
2023-02-15 01:38:04 +00:00
}
}