using System.Security.Cryptography;
using System.Text;

namespace Lumi.Companion.SongOverlay;

internal sealed class SongOverlaySecretProtector
{
    private static readonly byte[] Entropy = "Lumi.Companion.SongOverlay.v1"u8.ToArray();

    public string Protect(string value)
    {
        if (string.IsNullOrWhiteSpace(value)) return "";
        return Convert.ToBase64String(ProtectedData.Protect(Encoding.UTF8.GetBytes(value), Entropy, DataProtectionScope.CurrentUser));
    }

    public string Unprotect(string value)
    {
        if (string.IsNullOrWhiteSpace(value)) return "";
        try
        {
            return Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(value), Entropy, DataProtectionScope.CurrentUser));
        }
        catch (CryptographicException)
        {
            return "";
        }
    }
}
