/// <summary> /// Make and return a license for the given <seealso cref="LicenseData"/>. </summary> /// <param name="licenseData"> /// @return </param> /// <exception cref="LicenseGeneratorException"> If the generation encounters an error, usually due to invalid input. </exception> /// <exception cref="IllegalStateException"> If the generator is not setup correctly to make licenses. </exception> public string makeLicense(LicenseData licenseData) { if (!CanMakeLicenses) { throw new System.InvalidOperationException("The LicenseGenerator cannot make licenses as it was not configured with a private key"); } try { // var dsa = SignerUtilities.GetSigner("SHA1withDSA"); dsa.Init(true, privateKey); // string stringData = licenseData.toLicenseStringData(); byte[] licBytes = Encoding.UTF8.GetBytes(stringData); dsa.BlockUpdate(licBytes, 0, licBytes.Length); // byte[] signed = dsa.GenerateSignature(); string license = ToLicenseKey(signed); return(license); } catch (Exception e) { throw new LicenseGeneratorException(e); } }
/// <summary> /// Verify the given license for the given <seealso cref="LicenseData"/>. </summary> /// <param name="licenseData"> </param> /// <param name="license"> </param> /// <returns> Whether the license verified successfully. </returns> /// <exception cref="LicenseGeneratorException"> If the verification encounters an error, usually due to invalid input. You MUST check the return value of this method if no exception is thrown. </exception> /// <exception cref="IllegalStateException"> If the generator is not setup correctly to verify licenses. </exception> public virtual bool verifyLicense(LicenseData licenseData, string license) { if (!CanVerifyLicenses) { throw new System.InvalidOperationException("The LicenseGenerator cannot verify licenses as it was not configured with a public key"); } try { //Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); var dsa = SignerUtilities.GetSigner("SHA1withDSA"); dsa.Init(false, publicKey); // string stringData = licenseData.toLicenseStringData(); byte[] msgBytes = Encoding.UTF8.GetBytes(stringData); dsa.BlockUpdate(msgBytes, 0, msgBytes.Length); var dec = FromLicenseKey(license); var retVal = dsa.VerifySignature(dec); // return(retVal); } catch (InvalidKeyException e) { throw new LicenseGeneratorException(e); } catch (SignatureException e) { throw new LicenseGeneratorException(e); } catch (Exception e) { throw new LicenseGeneratorException(e); } }