把 secret.json 的内容从明文改为加密存储

This commit is contained in:
HerobrineXia committed 2026-09-08 21:19:45 +08:00
1 parent 3ed3a98bc3
commit c20feff8ab
2 files changed
+14 -4

No files matched your search

+13 -3
View File
@@ -118,7 +118,6 @@ public class JsonConfigProvider : IConfigProvider {
public static class ConfigProviders {
public static JsonConfigProvider Default = new(Path.Combine(Paths.AppDataThenName, "config.json"));
public static readonly JsonConfigProvider Secret = new(Path.Combine(Paths.AppData, "secret.json"));
}
#endregion
@@ -224,8 +223,19 @@ public static class ConfigUtils {
}
}
/// <summary>从 secret.json 中读取特定密钥,如果未找到对应密钥则抛出异常。</summary>
private static readonly Lazy<JObject> secret = new(() => {
string filePath = Path.Combine(Paths.AppData, "Meloong", "Dev", "secret.json");
try {
return FileUtils.Exists(filePath)
? JObject.Parse(CryptographyUtils.AesDecrypt(FileUtils.ReadAsString(filePath))!) : [];
} catch {
// 解密及 JSON 异常可能包含凭据内容,不保留原始异常,也不回退空配置。
throw new InvalidDataException($"读取密钥文件失败({filePath}),请检查文件内容与加密格式。");
}
});
/// <summary>从加密的 secret.json 中读取特定密钥,首次读取后缓存;如果未找到对应密钥则抛出异常。</summary>
public static string GetSecret(string key)
=> ConfigProviders.Secret.Read<string>(key, null, false) ?? throw new KeyNotFoundException($"未找到密钥:{key}");
=> secret.Value.Value<string>(key) ?? throw new KeyNotFoundException($"未找到密钥:{key}");
}