using System.Security.Cryptography;
using System.Text;
using ApiOPE.Configuration;
using Microsoft.Extensions.Options;

namespace ApiOPE.Security;

public sealed class OpeResponseSigner
{
    private readonly byte[] _secretKey;

    public OpeResponseSigner(IOptions<OpeOptions> options)
    {
        _secretKey = Encoding.UTF8.GetBytes(options.Value.SecretKey);
    }

    public string Sign(string uuid)
    {
        var hash = HMACSHA256.HashData(_secretKey, Encoding.UTF8.GetBytes(uuid));
        var lowercaseHex = Convert.ToHexString(hash).ToLowerInvariant();
        return Convert.ToBase64String(Encoding.UTF8.GetBytes(lowercaseHex));
    }
}
