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

55 lines
1.4 KiB
C#

using System;
using System.Security.Cryptography;
using System.Text;
namespace LevelCode.License.Security
{
public static class HashService
{
// =========================
// SHA-256 (string)
// =========================
public static string GerarSha256(string valor)
{
if (string.IsNullOrEmpty(valor))
return null;
using (SHA256 sha = SHA256.Create())
{
byte[] bytes = Encoding.UTF8.GetBytes(valor);
byte[] hash = sha.ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
}
// =========================
// SHA-256 (byte[])
// =========================
public static string GerarSha256(byte[] dados)
{
if (dados == null || dados.Length == 0)
return null;
using (SHA256 sha = SHA256.Create())
{
byte[] hash = sha.ComputeHash(dados);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
}
}
}