Commit 6d3ceca1 authored by Patrik Dufresne's avatar Patrik Dufresne

Add utility function validate() to LicenceManager.

parent 3c19f321
...@@ -78,10 +78,8 @@ public class KeyManager { ...@@ -78,10 +78,8 @@ public class KeyManager {
final int b1 = b % 3; final int b1 = b % 3;
if (a1 % 2 == 0) { if (a1 % 2 == 0) {
return (byte) (((seed >> a1) & 0x000000FF) ^ ((seed >> b1) | c)); return (byte) (((seed >> a1) & 0x000000FF) ^ ((seed >> b1) | c));
} else {
return (byte) (((seed >> a1) & 0x000000FF) ^ ((seed >> b1) & c));
} }
return (byte) (((seed >> a1) & 0x000000FF) ^ ((seed >> b1) & c));
} }
/** /**
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package com.patrikdufresne.license; package com.patrikdufresne.license;
import java.io.Serializable;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -23,7 +22,6 @@ import java.util.Collections; ...@@ -23,7 +22,6 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
......
...@@ -15,18 +15,12 @@ ...@@ -15,18 +15,12 @@
*/ */
package com.patrikdufresne.license; package com.patrikdufresne.license;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
...@@ -40,9 +34,11 @@ import java.util.Map.Entry; ...@@ -40,9 +34,11 @@ import java.util.Map.Entry;
import java.util.Properties; import java.util.Properties;
/** /**
* This the main entry point of the licensing module. This class should be used to create and check license files. * This the main entry point of the licensing module. This class should be used
* to create and check license files.
* <p> * <p>
* Generally, an application will not required more then one instance of license manager. * Generally, an application will not required more then one instance of license
* manager.
* *
* @author Patrik Dufresne * @author Patrik Dufresne
* *
...@@ -58,8 +54,39 @@ public class LicenseManager { ...@@ -58,8 +54,39 @@ public class LicenseManager {
private EncryptionManager encryptionManager; private EncryptionManager encryptionManager;
/** /**
* Create a new license manager. Generally, an application will not required more then one instance of license * Utility function to easily validate a license file.
* manager. */
public static License validate(InputStream publicKey, File... files) throws LicenseException {
if (publicKey == null || files == null || files.length == 0) {
throw new IllegalArgumentException();
}
// Create a new license manager.
LicenseManager licenseManager;
try {
licenseManager = new LicenseManager(publicKey, null);
} catch (Exception e) {
throw new LicenseException("invalid public key", e);
}
// Validate each license file.
LicenseException lastException = null;
for (File f : files) {
License license;
try {
license = licenseManager.readLicenseFile(f);
license.validate(new Date(), null);
return license;
} catch (LicenseException e) {
lastException = e;
} catch (Exception e) {
lastException = new LicenseException("invalid licence file", e);
}
}
throw lastException;
}
/**
* Create a new license manager. Generally, an application will not required
* more then one instance of license manager.
* *
* @param publicKey * @param publicKey
* the public key (can't be null). * the public key (can't be null).
...@@ -123,12 +150,7 @@ public class LicenseManager { ...@@ -123,12 +150,7 @@ public class LicenseManager {
* if the file doesn't exists * if the file doesn't exists
*/ */
public LicenseManager(String publicKey, String privateKey) throws GeneralSecurityException, IOException { public LicenseManager(String publicKey, String privateKey) throws GeneralSecurityException, IOException {
byte[] pubdata = EncryptionManager.readAll(new File(publicKey)); this(new File(publicKey), new File(privateKey));
byte[] privdata = null;
if (privateKey != null) {
privdata = EncryptionManager.readAll(new File(privateKey));
}
this.encryptionManager = new EncryptionManager(pubdata, privdata);
} }
/** /**
...@@ -140,13 +162,15 @@ public class LicenseManager { ...@@ -140,13 +162,15 @@ public class LicenseManager {
* @throws IOException * @throws IOException
* if file not found or read error. * if file not found or read error.
* @throws SignatureException * @throws SignatureException
* if this signature algorithm is unable to process the content of the file * if this signature algorithm is unable to process the content
* of the file
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
* if the SHA algorithm doesn't exists * if the SHA algorithm doesn't exists
* @throws InvalidKeyException * @throws InvalidKeyException
* if the public key is invalid * if the public key is invalid
* @throws ClassNotFoundException * @throws ClassNotFoundException
* if the implementation of {@link License} stored in the file can't be found * if the implementation of {@link License} stored in the file
* can't be found
*/ */
public License readLicenseFile(File file) throws IOException, InvalidKeyException, NoSuchAlgorithmException, SignatureException, ClassNotFoundException { public License readLicenseFile(File file) throws IOException, InvalidKeyException, NoSuchAlgorithmException, SignatureException, ClassNotFoundException {
...@@ -187,11 +211,13 @@ public class LicenseManager { ...@@ -187,11 +211,13 @@ public class LicenseManager {
* @param license * @param license
* the license object. * the license object.
* @param file * @param file
* the location where to save the new license file. If file exists, it's overwrite. * the location where to save the new license file. If file
* exists, it's overwrite.
* @throws IOException * @throws IOException
* if the file doesn't exists or can't be written to * if the file doesn't exists or can't be written to
* @throws SignatureException * @throws SignatureException
* if this signature algorithm is unable to process the license data * if this signature algorithm is unable to process the license
* data
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
* if the algorithm SHA is not supported * if the algorithm SHA is not supported
* @throws InvalidKeyException * @throws InvalidKeyException
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment