We hope this helps:
package com.lm.lic.manager.management;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
import com.lm.lic.manager.hibernate.HashedLicMechAlgorithm;
import com.lm.lic.manager.hibernate.HashedLicMechCharSet;
import com.lm.lic.manager.hibernate.HashedLicMechConfig;
import com.lm.lic.manager.hibernate.HashedLicMechKeyBase;
import com.lm.lic.manager.hibernate.HashedLicMechKeyFormat;
import com.lm.lic.manager.hibernate.HashedLicMechSegment;
import com.lm.lic.manager.hibernate.LicensePaymentStatus;
import com.sun.xml.ws.util.Base64Util;
/**
* @author Ibrahim Mustafa
*/
public class HashedLicKeyGenerator extends BasicLicKeyGenerator {
private static Logger logger = Logger.getLogger(HashedLicKeyGenerator.class);
/**
*
*/
public HashedLicKeyGenerator() {
}
/**
*
*
* @param deviceId
* @param num number of licenses to generate
* @param hlmConfig
* @param lps
* @return
*/
public List<String> generate(String deviceId, int num, HashedLicMechConfig hlmConfig, LicensePaymentStatus lps) {
List<String> licenses = new ArrayList<String>(num);
for(int i = 0; i < num; i++) {
String lic = generate(deviceId, hlmConfig, lps);
licenses.add(lic);
}
logger.info("Generated " + num + " lic keys");
return licenses;
}
/**
* For initial set of licenses that are for free, the deviceId is empty. Generate a random all-caps string
* in its place. The generated license is going to be replaced with that built from the combination of the
* real deviceId and the secret key the user chooses when adding a product instance.
*
* @param deviceId
* @param hlmConfig
* @param lps
* @return
*/
public String generate(String deviceId, HashedLicMechConfig hlmConfig, LicensePaymentStatus lps) {
String hashedKey = hlmConfig.getMessage();
StringBuilder sb = new StringBuilder(hlmConfig.getMessage());
if(!StringUtils.hasText(deviceId) && lps != null && lps.isFree()) {
deviceId = generate(hlmConfig.getLength());
}
if(StringUtils.hasText(deviceId)) {
// Append the deviceId in the same place of the intended wanted segment of the hashed key.
// If the segment is to the LAST or WHOLE of the hashed key, append the deviceId to the LAST
// of the secret key/message. Otherwise, if segment is START, append it to the left of it.
String segment = hlmConfig.getSegment();
if(segment.equals(
HashedLicMechSegment.START.name()))
sb.insert(0, deviceId);
else
sb.append(deviceId);
hashedKey = hashit(sb.toString(), hlmConfig);
}
return hashedKey;
}
private String hashit(String message, HashedLicMechConfig hlmConfig) {
MessageDigest digester = null;
String hashedKey = null;
try {
digester = MessageDigest.getInstance(HashedLicMechAlgorithm.getSecondName(hlmConfig.getAlgorithm()));
MessageDigest cd = (MessageDigest)digester.clone();
cd.update(message.getBytes(HashedLicMechCharSet.getSecondName(hlmConfig.getCharSet())));
byte[] hashBytes = cd.digest();
String keyFormat = hlmConfig.getKeyFormat();
String hashText = EMPTY;
if(HashedLicMechKeyFormat.isHex(keyFormat)) {
hashText = toHexString(hashBytes);
}
else if(HashedLicMechKeyFormat.isAscii(keyFormat)) {
if(HashedLicMechKeyBase.isBase64(hlmConfig.getBase()))
hashText = Base64Util.encode(hashBytes);
if(HashedLicMechKeyBase.isBase32(hlmConfig.getBase()))
hashText = Base32.encode(hashBytes);
else if(HashedLicMechKeyBase.isBase16(hlmConfig.getBase()))
hashText = toHexString(hashBytes);
}
hashedKey = extractSegment(hlmConfig.getSegment(), hlmConfig.getLength(), hashText);
} catch(NoSuchAlgorithmException e) {
e.printStackTrace();
} catch(UnsupportedEncodingException e) {
System.out.println("The Encoding Is Not Supported");
} catch(CloneNotSupportedException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
return hashedKey;
}
public String extractSegment(String segment, Integer length, String hashText) {
if(hashText == null || hashText.length() <= length)
return hashText;
String subtext = hashText;
if(segment.equals(
HashedLicMechSegment.START.name())) {
if(hashText.length() >= length)
subtext = hashText.substring(0, length);
else
subtext = hashText.substring(0, hashText.length());
}
else if(segment.equals(
HashedLicMechSegment.LAST.name())) {
if(hashText.length() >= length && hashText.length() <= 2 * length)
subtext = hashText.substring(length);
else
subtext = hashText.substring(hashText.length() - length);
}
return subtext;
}
}