public void TryRandomKeyGeneration() { string privateXmlKey = KeyHelpers.GetPrivateKey(); ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey); string productKey = keyPublisher.GenerateProductKey(0, 0, 0, "", "", "", ""); Console.WriteLine(productKey); keyPublisher.ValidateProductKey(productKey); for (int n = 1; n < 0x100; n++) { byte[] bytes = BitConverter.GetBytes(n); string fakeSegment = Convert.ToBase64String(new byte[] { bytes[2], bytes[1], bytes[0] }); if (productKey.Length > 6 && fakeSegment != productKey.Substring(6)) { productKey = String.Format("{0}{1}", fakeSegment, productKey.Substring(5)); try { keyPublisher.ValidateProductKey(productKey); Assert.Fail("This should raise an InvalidProductKeyException"); } catch (InvalidProductKeyException) { // OK. We are expecting this exception. } catch (Exception ex) { throw ex; } } } }
public void ValidateProductKey() { string privateXmlKey = KeyHelpers.GetPrivateKey(); ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey); string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**"); Console.WriteLine(productKey); keyPublisher = new ProductKeyPublisher(privateXmlKey); ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey); var shaHasher = new SHA1CryptoServiceProvider(); var usernameHash = Encoding.ASCII.GetString(shaHasher.ComputeHash(Encoding.ASCII.GetBytes("toto"))); var companyHash = Encoding.ASCII.GetString(shaHasher.ComputeHash(Encoding.ASCII.GetBytes("Company"))); var userEmailHash = Encoding.ASCII.GetString(shaHasher.ComputeHash(Encoding.ASCII.GetBytes("*****@*****.**"))); Assert.AreEqual(123, result.ProductID); Assert.AreEqual(456, result.ProductFeatures); Assert.AreEqual(789, result.TrialDays); Assert.AreEqual(usernameHash, result.UsernameHash); Assert.AreEqual(companyHash, result.CompanyHash); Assert.AreEqual(userEmailHash, result.UserEmailHash); Assert.AreEqual(DateTime.Now.Date, result.GeneratedDate.Date); }
public void TryValidateWithPublicKey() { string privateXmlKey = KeyHelpers.GetPrivateKey(); ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey); string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**"); Console.WriteLine(productKey); privateXmlKey = KeyHelpers.GetPublicKey(); keyPublisher = new ProductKeyPublisher(privateXmlKey); ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey); }
public void TryValidateWithFakeCryptProvider() { string privateXmlKey = KeyHelpers.GetPrivateKey(); ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey); string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**"); Console.WriteLine(productKey); privateXmlKey = (new RSACryptoServiceProvider()).ToXmlString(true); keyPublisher = new ProductKeyPublisher(privateXmlKey); ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey); }
public void ValidateFakeProductKey() { string privateXmlKey = KeyHelpers.GetPrivateKey(); ProductKeyPublisher keyPublisher = new ProductKeyPublisher(privateXmlKey); string productKey = keyPublisher.GenerateProductKey(123, 456, 789, "C", "toto", "Company", "*****@*****.**"); Console.WriteLine(productKey); productKey = productKey.Replace('1', '8'); productKey = productKey.Replace('2', '7'); productKey = productKey.Replace('3', '6'); productKey = productKey.Replace('4', '5'); Console.WriteLine(productKey); keyPublisher = new ProductKeyPublisher(privateXmlKey); ProductKeyInfo result = keyPublisher.ValidateProductKey(productKey); }