Andru
2023-10-27 ff57ace362af07655f54eea90c4d2f63b05f5b66
src/main/java/com/hx/encryption/PTEncryptionUtil.java
@@ -1,6 +1,8 @@
package com.hx.encryption;
import com.hx.util.DateUtil;
import com.hx.util.MD5Util;
import com.hx.util.StringUtils;
import java.util.Calendar;
import java.util.Date;
@@ -12,6 +14,7 @@
 */
public class PTEncryptionUtil {
    /*
     * 日期加密
     * date  要加密的日期
@@ -19,24 +22,55 @@
    public static String  encryption(Date date){
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(Calendar.YEAR);//获取年份
        int month=cal.get(Calendar.MONTH);//获取月份
        int day=cal.get(Calendar.DATE);//获取日
        int hour=cal.get(Calendar.HOUR_OF_DAY);//小时
        int minute=cal.get(Calendar.MINUTE);//分
        int second=cal.get(Calendar.SECOND);//秒
        StringBuffer data = new StringBuffer();
        data.append(cal.get(Calendar.DATE));
        data.append(cal.get(Calendar.MONTH));
        data.append(cal.get(Calendar.YEAR));
        data.append(cal.get(Calendar.SECOND));
        data.append(cal.get(Calendar.MINUTE));
        data.append(cal.get(Calendar.HOUR_OF_DAY));
        String yearStr = com.hx.mp.util.MD5Util.MD5Encode(year+"","");
        String monthStr = com.hx.mp.util.MD5Util.MD5Encode(month+"","");
        String dayStr = com.hx.mp.util.MD5Util.MD5Encode(day+"","");
        String hourStr = com.hx.mp.util.MD5Util.MD5Encode(hour+"","");
        String minuteStr = com.hx.mp.util.MD5Util.MD5Encode(minute+"","");
        String secondStr = com.hx.mp.util.MD5Util.MD5Encode(second+"","");
        StringBuffer result = new StringBuffer();
        result.append(yearStr).append("-").append(monthStr).append("-").append(dayStr).append("-")
                .append(hourStr).append("-").append(minuteStr).append("-").append(secondStr);
        return result.toString();
        return com.hx.mp.util.MD5Util.MD5Encode(data.toString(),"");
    }
    /**
     *
     * @param data 签名
     * @param date 时间
     * @param time 有效时间 秒
     * @return
     */
    public static PTConstant decode(String data,Date date,Integer time){
        PTConstant ptConstant = new PTConstant();
        ptConstant.setSuccess(true);
        if (StringUtils.isEmpty(data)){
            ptConstant.setSuccess(false);
            ptConstant.setErrCode(PTConstant.PT_SIGN_EEMPLY);
            return ptConstant;
        }
        if (null == date){
            ptConstant.setSuccess(false);
            ptConstant.setErrCode(PTConstant.PT_TIMELS_EMPLY);
            return ptConstant;
        }
        if(!encryption(date).equals(data)){
            ptConstant.setSuccess(false);
            ptConstant.setErrCode(PTConstant.PT_SIGN_ERROR);
            return ptConstant;
        }
        if(time == null){
            ptConstant.setSuccess(true);
            return ptConstant;
        }
        //有效时间判断
        if (DateUtil.calLastedTime(date) > time){
            ptConstant.setSuccess(false);
            ptConstant.setErrCode(PTConstant.PT_ORER_TIME);
            return ptConstant;
        }
        return ptConstant;
    }
}