public void Checksum16SmallDataSet() { int checksum = provider.Checksum16(checksumTest.Replace("-", "")); string hex = checksum.ToString("X"); Assert.AreEqual(checksumNumber, checksum); Assert.AreEqual(checksumHash, hex); }
public string GenerateLicenseKey(string rsaXmlString, LicenseBase scutexLicense, LicenseGenerationOptions generationOptions) { // Init all required variables for the process. Dictionary <int, LicensePlaceholder> placeholerLocations; List <LicensePlaceholder> licensePlaceholders = CreateLicensePlaceholders(scutexLicense, generationOptions); string licenseKey; char[] licenseKeyArray; // Setup the license key to work on licenseKey = licenseKeyTemplate.Replace("-", ""); // Locate all the placeholder tokens in the license template placeholerLocations = FindAllPlaceholdersInTemplate(licenseKey, licensePlaceholders); // Verify that all the registered placeholders were located in the template. if (placeholerLocations.Count != licensePlaceholders.Count) { throw new Exception(string.Format(Resources.ErrorMsg_PlaceholderCount, licensePlaceholders.Count, placeholerLocations.Count)); } // Change all non-checksum placeholders to their actual values in the key foreach (var p in placeholerLocations) { if (!p.Value.IsChecksum) { string token = ""; for (int i = 0; i < p.Value.Length; i++) { token = token + p.Value.Token; } licenseKey = licenseKey.Replace(token, p.Value.Value); } } // Compute and change the random license key placeholders licenseKeyArray = licenseKey.ToCharArray(); for (int i = 0; i < licenseKeyArray.Length; i++) { if (licenseKeyArray[i] == char.Parse("x")) { licenseKeyArray[i] = GetRandomCharacter(); } } licenseKey = new string(licenseKeyArray); // Obfuscate key license placeholders that are not checksums foreach (var p in placeholerLocations) { if (!p.Value.IsChecksum) { if (p.Value.Type == PlaceholderTypes.Number) { //Console.WriteLine(p.Value.Token); //Console.WriteLine("-----------------"); licenseKeyArray = licenseKey.ToCharArray(); for (int i = 0; i < p.Value.Length; i++) { char previousChar = licenseKeyArray[(p.Key) - 1]; int data = int.Parse(licenseKeyArray[p.Key + i].ToString(), NumberStyles.HexNumber); char obfKey = KeyIntegerValueObfuscator(previousChar, data, p.Key + i); licenseKeyArray[p.Key + i] = obfKey; } licenseKey = new string(licenseKeyArray); } else if (p.Value.Type == PlaceholderTypes.String) { licenseKeyArray = licenseKey.ToCharArray(); for (int i = 0; i < p.Value.Length; i++) { char previousChar = licenseKeyArray[(p.Key) - 1]; int modData = CharacterMap.ReverseMap[licenseKeyArray[p.Key + i]]; //Console.WriteLine(string.Format("Char: {0}", licenseKeyArray[p.Key + i])); char obfKey = KeyIntegerValueObfuscator(previousChar, modData, p.Key + i); licenseKeyArray[p.Key + i] = obfKey; } licenseKey = new string(licenseKeyArray); } } } // Now compute and change all the checksum placeholders in the key foreach (var p in placeholerLocations) { if (p.Value.IsChecksum) { string token = ""; for (int i = 0; i < p.Value.Length; i++) { token = token + p.Value.Token; } string hash = hashingProvider.Checksum16(licenseKey.Substring(0, p.Key)).ToString("X"); hash = hash.PadLeft(4, char.Parse("0")); licenseKey = licenseKey.Replace(token, hash); } } // Insert the seperators List <int> seperatorLocations = FindAllSeperatorsInTemplate(licenseKeyTemplate); string finalKey = licenseKey; if (seperatorLocations.Count > 0) { int remaining = seperatorLocations.Count - 1; for (int i = 0; i < seperatorLocations.Count; i++) { finalKey = finalKey.Insert(seperatorLocations[i] - remaining, "-"); remaining--; } } return(finalKey); }
internal List <LicensePlaceholder> CreateLicensePlaceholders(LicenseBase scutexLicense, LicenseGenerationOptions generationOptions) { List <LicensePlaceholder> Placeholders = new List <LicensePlaceholder>(); if (generationOptions != null) { Placeholders.Add(new LicensePlaceholder { Length = 1, Token = Char.Parse("k"), Type = PlaceholderTypes.Number, Value = ((int)generationOptions.LicenseKeyType).ToString(), IsChecksum = false, ValidationType = ValidationTypes.LicenseKeyType }); } else { Placeholders.Add(new LicensePlaceholder { Length = 1, Token = Char.Parse("k"), Type = PlaceholderTypes.Number, Value = "0", IsChecksum = false, ValidationType = ValidationTypes.LicenseKeyType }); } Placeholders.Add(new LicensePlaceholder { Length = 2, Token = Char.Parse("a"), Type = PlaceholderTypes.Number, Value = scutexLicense.Product.GetFormattedProductId(2), IsChecksum = false, ValidationType = ValidationTypes.None }); Placeholders.Add(new LicensePlaceholder { Length = 1, Token = Char.Parse("c"), Type = PlaceholderTypes.Number, Value = "", IsChecksum = true, ValidationType = ValidationTypes.None }); string licProdChecksum = hashingProvider.Checksum16(scutexLicense.GetLicenseProductIdentifier()).ToString("X"); if (licProdChecksum.Length < 4) { licProdChecksum = licProdChecksum.PadLeft(4, char.Parse("0")); } Placeholders.Add(new LicensePlaceholder { Length = 4, Token = Char.Parse("p"), Type = PlaceholderTypes.Number, Value = licProdChecksum, IsChecksum = false, ValidationType = ValidationTypes.None }); return(Placeholders); }