using System.Security.Cryptography;
public static void Main()
var base64Secret = "+bCzRidhVxwx5TKpkz14nf5cL+BMH+ahZIOCy4bPVzdXWhysq+tZfOtrHsw9vdg5vKes/lVwzHIyquvmz9taDg==";
var licenseKey = CreateLicense(base64Secret);
var isValid = ValidateLicense(licenseKey, base64Secret);
Console.WriteLine($"Is Licsense Valid = {isValid}");
static string CreatePrivateKey()
byte[] hashKey = GenerateRandomCryptographicBytes(64);
var base64Secret = Convert.ToBase64String(hashKey);
Console.WriteLine($"Private Key = {base64Secret}");
static string CreateLicense(string secretKey)
var licenseKey = Guid.NewGuid().ToString().ToUpper().Replace("-", "").Substring(0, 15);
Console.WriteLine($"licenseKey = {licenseKey}");
var storedHmacOnDB = CalculateHmac(licenseKey, secretKey).ToUpper();
var HMACTruncated = storedHmacOnDB.Substring(0, 15);
Console.WriteLine($"HMAC = {HMACTruncated}");
var licenseAndHMAC = InsertHyphen($"{licenseKey}{HMACTruncated}");
Console.WriteLine($"Final User Licsense = {licenseAndHMAC}");
static bool ValidateLicense(string licenseKey, string secretKey)
var tmp = licenseKey.Split('-');
var license = $"{tmp[0]}{tmp[1]}{tmp[2]}";
var licenseHmac = $"{tmp[3]}{tmp[4]}{tmp[5]}";
string calculatedHmac = CalculateHmac(license, secretKey);
var HMACTruncated = calculatedHmac.ToUpper().Substring(0, 15);
bool isValid = licenseHmac.Equals(HMACTruncated, StringComparison.OrdinalIgnoreCase);
static string InsertHyphen(string input, int everyNthChar = 5)
var sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
if ((i + 1) % everyNthChar == 0 && i != input.Length - 1)
static byte[] GenerateRandomCryptographicBytes(int keyLength)
byte[] key = new byte[64];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
static string CalculateHmac(string data, string hashKeyBase64)
var byteArray = Convert.FromBase64String(hashKeyBase64);
return CalculateHmac(data, byteArray);
static string CalculateHmac(string data, byte[] hashKey)
var hmac = new HMACMD5(hashKey);
byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(data));
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();