com.mqm.frame.infrastructure.util.ValidateLicense.java Source code

Java tutorial

Introduction

Here is the source code for com.mqm.frame.infrastructure.util.ValidateLicense.java

Source

/**
 * Copyright(c) Foresee Science & Technology Ltd. 
 */
package com.mqm.frame.infrastructure.util;

import java.io.FileInputStream;
import java.security.PublicKey;
import java.security.Signature;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mqm.frame.util.DateTimeUtil;
import com.mqm.frame.util.InternationalizationUtil;
import com.mqm.frame.util.license.CollectMacAddress;
import com.mqm.frame.util.license.LicenseColl;
import com.mqm.frame.util.license.LicenseInfo;
import com.sun.org.apache.xml.internal.security.utils.Base64;

/**
 * <pre>
 * ?License
 * </pre>
 * 
 * @author luxiaocheng luxiaocheng@foresee.cn
 * @version 1.00.00
 * 
 *          <pre>
 * 
 *    ?:       :     :
 * </pre>
 */
public class ValidateLicense {

    private static final Log log = LogFactory.getLog(ValidateLicense.class);

    /**
     * License
     * 
     * @param licenseColl LicenseColl
     * @param path String
     * @return boolean
     */
    public static boolean validateLicense(LicenseColl licenseColl, String path) {
        // ??
        boolean payedUser = true;
        List<String> macAddressList = CollectMacAddress.getMacAddress();
        for (LicenseInfo licenseInfo : licenseColl.getLicenseInfoList()) {
            String productName = licenseInfo.getProductName();
            String expirationDate = licenseInfo.getExpiration();
            String signature = licenseInfo.getSignature();
            boolean flag = false;
            String data = new StringBuffer().append(productName).append(expirationDate).toString();
            java.security.cert.CertificateFactory cf;
            try {
                cf = java.security.cert.CertificateFactory.getInstance("X.509");

                java.security.cert.Certificate cert = cf
                        .generateCertificate(new FileInputStream(path + "/WEB-INF/fbrp.cer"));

                // 
                PublicKey pubKey = cert.getPublicKey();
                // ??Signature
                Signature sig = Signature.getInstance("SHA1withDSA");
                sig.initVerify(pubKey);
                sig.update(InternationalizationUtil.getBytes(data));
                // ?License
                boolean verifies = sig.verify(Base64.decode((InternationalizationUtil.getBytes(signature))));
                if (verifies) {
                    Date evalDate = DateTimeUtil.parseDate(expirationDate);
                    if (evalDate.before(new Date())) {
                        // ?
                        payedUser = false;
                    } else {
                        flag = true;
                        payedUser = true;
                        System.out.println(
                                "" + productName + " License" + expirationDate + "?");
                    }
                }
            } catch (Exception e) {
                log.error("", e);
            }

            if (!flag) {
                payedUser = false;
                System.out.println(
                        "" + productName + " License??License?");
            }
        }
        return payedUser;
    }

}