package com.fdkankan.sign;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.util.Date;

public class SignUtils {

    public static Boolean checkSign(String sign,String appIdValue,String privateKey) {
        try {
            if(StringUtils.isBlank(sign)){
                return false;
            }
            String deTxt = RsaUtils.decipher(sign, privateKey);
            if(StringUtils.isBlank(deTxt)){
                return false;
            }
            JSONObject jsonObject = new JSONObject(deTxt);
            String appId = jsonObject.getString("appId");
            Long timestamp = jsonObject.getLong("timestamp");
            if(StringUtils.isBlank(appId) || timestamp == null){
                return false;
            }
            if(!appId.equals(appIdValue)){
                return false;
            }
            Long time = new Date().getTime();

//            if(time < timestamp && timestamp - time > 1000 * 60){
//                return false;
//            }
            //app获取时间存在误差，前后五分钟失败
            if( Math.abs(time -timestamp) >1000 * 60 *5){
                return false;
            }
            return true;
        }catch (Exception e){
            return false;
        }
    }

}
