LevelcodeLicenseAPP/Security/LicenseEngine.cs
2026-03-25 16:26:11 -03:00

51 lines
1.5 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
namespace LevelCode.License.Security
{
public static class LicenseEngine
{
// =========================
// GERAR ARQUIVO .KEY
// =========================
public static void GerarArquivoKey(
LicensePayload payload,
string chavePrivada,
string caminhoArquivo)
{
if (payload == null)
throw new ArgumentNullException(nameof(payload));
if (string.IsNullOrWhiteSpace(chavePrivada))
throw new ArgumentException("Chave privada inválida.");
// 1⃣ Serializa payload (somente dados)
string json = JsonSerializer.Serialize(payload);
byte[] dados = Encoding.UTF8.GetBytes(json);
// 2⃣ Assina os dados
byte[] assinatura;
using (RSA rsa = RSA.Create())
{
rsa.FromXmlString(chavePrivada);
assinatura = rsa.SignData(
dados,
HashAlgorithmName.SHA256,
RSASignaturePadding.Pkcs1);
}
// 3⃣ Monta o arquivo .key
string conteudoFinal =
Convert.ToBase64String(dados) +
"|SIGN|" +
Convert.ToBase64String(assinatura);
// 4⃣ Salva o arquivo
File.WriteAllText(caminhoArquivo, conteudoFinal, Encoding.UTF8);
}
}
}