51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|