license生成工具

本文介绍了一个专注于使用Java语言开发的许可证生成工具,该工具能够帮助开发者便捷地创建和管理项目的授权许可文件。
摘要由CSDN通过智能技术生成

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;

/**
 * 产品license生成工具
 */
public class LicenseUtil {

    private String ip;

    //生成license
    public String createLicense(){
        return RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "-" +
                RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "-" +
                RandomStringUtils.randomAlphanumeric(4).toUpperCase() + "-" +
                RandomStringUtils.randomAlphanumeric(4).toUpperCase();
    }


    //生成license文件
    public void createLicenseFile(String mac, Integer effectDays, File file, String key, String licenseStr) throws IOException {
        //生成license文件包含mac、生成license时间、license有效时间、鉴权结果,内容通过des加密、aes加密
        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));
    }

    //激活license
    public Boolean actLicense(String userLicenseStr, File file, String key) throws IOException {
        //获取加密的license值
        String encryptLicenseJson = FileUtils.readFileToString(file);
        //解密license映射实体
        License license = new Gson().fromJson(DESDecrypt(AESDecrypt(encryptLicenseJson,key),key), License.class);
        //对比用户填写的license是否正确
        if(StringUtils.equals(license.getLicenseStr(),userLicenseStr)){
            //激活license文件
            license.setAuth(true);
            String licenseJson = new Gson().toJson(license);
            FileUtils.writeStringToFile(file, AESEncrypt(DESEncrypt(licenseJson,key),key));
            return true;
        }
        return false;
    }

    //校验license
    public Boolean authLicense(File file, String key) throws IOException {
        //获取加密的license值
        String encryptLicenseJson = FileUtils.readFileToString(file);
        //解密license映射实体
        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;
    }

    //AES加密
    private String AESEncrypt(String content, String key){
        AES aes = new AES(key.getBytes());
        return aes.encryptHex(content);
    }

    //AES解密
    private String AESDecrypt(String content, String key){
        AES aes = new AES(key.getBytes());
        return aes.decryptStr(content);
    }

    //DES加密
    private String DESEncrypt(String content, String key){
        DES des = new DES(key.getBytes());
        return des.encryptHex(content);
    }

    //DES解密
    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;}
        /***去重,别忘了同一个网卡的ipv4,ipv6得到的mac都是一样的,肯定有重复,下面这段代码是。。流式处理***/
        List<String> unique = tmpMacList.stream().distinct().collect(Collectors.toList());

        System.out.println("服务器mac地址列表为:" + unique.toString());

        return unique;
    }

    private String toHexString(int integer) {
        // 将得来的int类型数字转化为十六进制数
        String str = Integer.toHexString((int) (integer & 0xff));
        // 如果遇到单字符,前置0占位补满两格
        if (str.length() == 1) {
            str = "0" + str;
        }
        return str;
    }

    @Getter
    @Setter
    private static class License{
        //mac地址
        private String mac;
        //license文件生成时间
        private Date createTime;
        //license有效天数
        private Integer effectDays;
        //是否激活
        private Boolean auth;
        //license
        private String licenseStr;
    }
}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值