This project provide a java library to be used by your java project to easily validate a license file.
-
You generate a private and public key.
-
You publish your application with this library and your public key.
-
You keep the private key for youself.
-
You generate a license file for your user with a deadline.
-
You are sending this file to your user (via email or similar).
-
Your user install this license file where your application can pick it up.
-
When your application start, this library is used to validate the license file and validate the deadline.
Create the private key:
openssl genrsa -out privkey.pem 2048
openssl pkcs8 -topk8 -in privkey.pem -inform PEM -nocrypt -outform DER -out privkey.der
Extract the public key, for publishing:
openssl rsa -in privkey.pem -out pubkey.der -pubout -outform DER
To make use of this license library, you must change a bit the implementation of your application. First, you must import the library. For maven project, you may edit the pom.xml
as follow:
<repositories>
<repository>
<id>patrikdufresne</id>
<url>http://nexus.patrikdufresne.com/content/groups/public/</url>
</repository>
</repositories>
...
<dependencies>
<dependency>
<groupId>com.patrikdufresne</groupId>
<artifactId>license</artifactId>
<version>0.11</version>
</dependency>
</dependencies>
In your java code, you must implement something similar.
License l;
try {
l = LicenseManager.validate(Main.class.getResourceAsStream("/pubkey.der"), new File("application.lic"));
} catch (LicenseException e) {
System.out.println("invalid license file:" + e.getMessage());
System.exit(1);
return;
}
System.out.println("License to: " + l.getProperty(License.NAME));
System.out.println("Expired: " + l.getProperty(License.EXPIRATION));
Take a loot at http://git.patrikdufresne.com/pdsl/license/blob/master/docs/examples/Main.java
To generate a license file you may use the jar it self in command line:
wget https://nexus.patrikdufresne.com/repository/public/com/patrikdufresne/license/0.11/license-0.11.jar
java -jar license-0.11.jar create -p pubkey.der -P privkey.der -n username -m test@example.com -e 2020-01-09
It's generate a license file named application.lic
.
This library is compatible with Java 7 and earlier. This library doesn't need any dependecies.