license生成工具
根据 mac地址,生成验证码以及license文件
package com.datacvg.util;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.DES;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class LicenseUtil {
private String ip;
public String createLicense(){
return RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "-" +
RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "-" +
RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "-" +
RandomStringUtils.randomAlphanumeric(4).toUpperCase();
}
public void createLicenseFile(String mac, Integer effectDays, File file, String key, String licenseStr) throws IOException {
License license = new License();
license.setMac(mac);
license.setAuth(false);
license.setCreateTime(new Date());
license.setEffectDays(effectDays);
license.setLicenseStr(licenseStr);
String licenseJson = new Gson().toJson(license);
FileUtils.writeStringToFile(file, AESEncrypt(DESEncrypt(licenseJson,key),key));
}
public Boolean actLicense(String userLicenseStr, File file, String key) throws IOException {
String encryptLicenseJson = FileUtils.readFileToString(file);
License license = new Gson().fromJson(DESDecrypt(AESDecrypt(encryptLicenseJson,key),key), License.class);
if(StringUtils.equals(license.getLicenseStr(),userLicenseStr)){
license.setAuth(true);
String licenseJson = new Gson().toJson(license);
FileUtils.writeStringToFile(file, AESEncrypt(DESEncrypt(licenseJson,key),key));
return true;
}
return false;
}
public Boolean authLicense(File file, String key) throws IOException {
String encryptLicenseJson = FileUtils.readFileToString(file);
License license = new Gson().fromJson(DESDecrypt(AESDecrypt(encryptLicenseJson,key),key), License.class);
if(license.getAuth()){
if (!getMacList().contains(license.getMac().toUpperCase())){
return false;
}
Date effectDate = DateUtils.addDays(license.getCreateTime(),license.getEffectDays());
Date now = new Date();
return effectDate.getTime() >= now.getTime();
}
return false;
}
private String AESEncrypt(String content, String key){
AES aes = new AES(key.getBytes());
return aes.encryptHex(content);
}
private String AESDecrypt(String content, String key){
AES aes = new AES(key.getBytes());
return aes.decryptStr(content);
}
private String DESEncrypt(String content, String key){
DES des = new DES(key.getBytes());
return des.encryptHex(content);
}
private String DESDecrypt(String content, String key){
DES des = new DES(key.getBytes());
return des.decryptStr(content);
}
public static List<String> getMacList() {
ArrayList<String> tmpMacList = new ArrayList<>();
try {
java.util.Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
StringBuilder sb = new StringBuilder();
while (en.hasMoreElements()) {
NetworkInterface iface = en.nextElement();
List<InterfaceAddress> addrs = iface.getInterfaceAddresses();
for (InterfaceAddress addr : addrs) {
InetAddress ip = addr.getAddress();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
continue;
}
byte[] mac = network.getHardwareAddress();
if (mac == null) {
continue;
}
sb.delete(0, sb.length());
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
tmpMacList.add(sb.toString().toUpperCase());
}
}
} catch(Exception e){
}
if(tmpMacList.size()<=0){return tmpMacList;}
List<String> unique = tmpMacList.stream().distinct().collect(Collectors.toList());
System.out.println("服务器mac地址列表为:" + unique.toString());
return unique;
}
private String toHexString(int integer) {
String str = Integer.toHexString((int) (integer & 0xff));
if (str.length() == 1) {
str = "0" + str;
}
return str;
}
@Getter
@Setter
private static class License{
private String mac;
private Date createTime;
private Integer effectDays;
private Boolean auth;
private String licenseStr;
}
}