Ejemplo n.º 1
0
        public void TestCheckCharacterGeneration()
        {
            char data = provider.GetChecksumChar(testSmallLicenseChecksumData);

            Assert.AreEqual(char.Parse("8"), data);
        }
Ejemplo n.º 2
0
        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 ScutexLicenseException(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 cheksums
            foreach (var p in placeholerLocations)
            {
                if (!p.Value.IsChecksum)
                {
                    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);
                }
            }

            // Now compute and change all the cheksum 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;
                    }

                    char hash = hashingProvider.GetChecksumChar((licenseKey.Substring(0, p.Key)));

                    licenseKey = licenseKey.Replace(token, hash.ToString());
                }
            }

            // 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);
        }