All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.founder.mip.DataHandler Maven / Gradle / Ivy

There is a newer version: 3.6.1.9
Show newest version
package com.founder.mip;

import com.alibaba.fastjson.JSONObject;
import com.founder.core.exception.FuncRetCode;
import com.founder.core.exception.MipServerException;
import com.founder.core.log.MyLog;
import com.founder.mip.utils.HseEncAndDecUtil;
import com.founder.mip.utils.HttpUtil;
import com.founder.mip.utils.SM2Util;
import com.founder.mip.vopackage.enc.HOSParamEncDTO;
import com.founder.mip.vopackage.enc.HOSResultEncDTO;

import java.io.IOException;

public class DataHandler {

    private static final MyLog LOG = MyLog.getLog(DataHandler.class);
    private static volatile DataHandler INSTANCE;
    private final String publicKeys;
    private final String privateKeys;
    private final String appID;
    private final String secret;
    private String version = "2.0.1";
    private String encType = "SM4";
    private String signType = "SM2";
    SM2Util sm2 = new SM2Util();

    private DataHandler(String appID, String secret, String publicKey, String privateKey) {
        this.appID = appID;
        this.secret = secret;
        this.publicKeys = publicKey;
        this.privateKeys = privateKey;
    }

    public static DataHandler newInstance(String appID, String secret, String publicKey, String privateKey) {
        if (INSTANCE == null) {
            Class var4 = DataHandler.class;
            synchronized(DataHandler.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DataHandler(appID, secret, publicKey, privateKey);
                }
            }
        }
        return INSTANCE;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setEncType(String encType) {
        this.encType = encType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }

    /**
     * 数据data节点
     * @param objData
     * @return
     * @throws Exception
     */
    public HOSParamEncDTO buildReqData(Object objData) {
        HOSParamEncDTO encDTO = new HOSParamEncDTO();
        String data = JSONObject.toJSONString(objData,false);
        LOG.info("加密原文:" + data);
        String timestamp = String.valueOf(System.currentTimeMillis());
        LOG.info("时间戳:" + timestamp);
        JSONObject object = new JSONObject();
        object.put("version", this.version);
        object.put("encType", this.encType);
        object.put("signType", this.signType);
        object.put("appId", this.appID);
        object.put("timestamp",timestamp);
        object.put("data", JSONObject.toJSON(objData));
        String signData = null;
        try {
            //object.put("data", JsonDealUtils.getNoNullValue(data));
            String jsonStr = JSONObject.toJSONString(object,false);
            LOG.info("签名原文:" + jsonStr);
            signData = HseEncAndDecUtil.signature(jsonStr,secret,privateKeys);
            LOG.info("签名值:" + signData);
            //object.fluentRemove("data");
        } catch (Exception e) {
            throw new MipServerException(FuncRetCode.ERROR_CODE_SIGN_ERROR);
        }
        //object.put("data", JSON.toJSON(objData));
        String encData = null;
        try {
            encData = HseEncAndDecUtil.sm4Encrypt(appID,secret,data);
            LOG.info("加密结果:" + encData);
        } catch (Exception e) {
            throw new MipServerException(FuncRetCode.ERROR_CODE_ENCRYPT_ERROR);
        }
        encDTO.setAppId(appID);//appId 渠道 id String(32) N
        encDTO.setVersion(version);//version 版本号 String(6) N 2.0.1
        encDTO.setTimestamp(timestamp);//timestamp 当前时间 String(32) N 时间戳
        encDTO.setEncType("SM4");//encType 加密方式 String(6) N SM4
        encDTO.setSignType("SM2");//signType 签名方式 String(6) N SM2
        encDTO.setEncData(encData);//encData 加密数据 String(max) N
        encDTO.setSignData(signData);//signData 签名串 String(200) N
        LOG.info("请求报文:" + JSONObject.toJSONString(encDTO,true));
        return encDTO;
    }

    /**
     * 组合加密返回参数 【回调用 暂时作废 回调传明文】
     * @param objData  数据data节点
     * @param code
     * @param message
     * @param success
     * @return
     */
    @Deprecated
    public HOSResultEncDTO buildRspData(Object objData, Integer code, String message, Boolean success) {
        HOSResultEncDTO encDTO = new HOSResultEncDTO();
        String data = JSONObject.toJSONString(objData,false);
        LOG.info("加密原文:" + data);
        String timestamp = String.valueOf(System.currentTimeMillis());
        LOG.info("时间戳:" + timestamp);
        JSONObject object = new JSONObject();
        object.put("code",code);//code 响应状态码 int N
        object.put("appId", this.appID);//appId 渠道 id String(32) N
        object.put("timestamp",timestamp);//timestamp 当前时间 String(32) N 时间戳
        object.put("encType", this.encType);//encType 加密方式 String(6) N SM4
        object.put("signType", this.signType);//signType 签名方式 String(6) N SM2
        object.put("message",message);//message 响应异常信息 String(max) Y
        object.put("success",success);//success 响应标识 boolean Y
        object.put("version", this.version);//version 版本号 String(6) N 2.0.1
        object.put("data", JSONObject.toJSON(objData));
        String signData = null;
        try {
            //object.put("data", JsonDealUtils.getNoNullValue(data));
            String jsonStr = JSONObject.toJSONString(object,false);
            LOG.info("出参签名原文:" + jsonStr);
            signData = HseEncAndDecUtil.signature(jsonStr,secret,privateKeys);
            LOG.info("签名值:" + signData);
            //object.fluentRemove("data");
        } catch (Exception e) {
            throw new MipServerException(FuncRetCode.ERROR_CODE_SIGN_ERROR);
        }
        String encData = null;
        try {
            encData = HseEncAndDecUtil.sm4Encrypt(appID,secret,data);
            LOG.info("加密出参结果:" + encData);
        } catch (Exception e) {
            throw new MipServerException(FuncRetCode.ERROR_CODE_ENCRYPT_ERROR);
        }
        encDTO.setCode(code);//code 响应状态码 int N
        encDTO.setAppId(this.appID);//appId 渠道 id String(32) N
        encDTO.setTimestamp(timestamp);//timestamp 当前时间 String(32) N 时间戳
        encDTO.setEncType(this.encType);//encType 加密方式 String(6) N SM4
        encDTO.setSignType(this.signType);//signType 签名方式 String(6) N SM2
        encDTO.setSignData(signData);//signData 签名串 String(200) N
        encDTO.setEncData(encData);//encData 加密数据 String(max) N
        encDTO.setMessage(message);//message 响应异常信息 String(max) Y
        encDTO.setSuccess(success);//success 响应标识 boolean Y
        encDTO.setVersion(this.version);//version 版本号 String(6) N 2.0.1
        return encDTO;
    }

    /**
     * 解密验签HOSParamEncDTO 【回调用 暂时作废 回调传明文】
     * @param encDTO
     * @return
     */
    @Deprecated
    public String processReqData(HOSParamEncDTO encDTO) {
        String rspData = JSONObject.toJSONString(encDTO,true);
        LOG.info("收到报文:" + rspData);
        JSONObject jsonObject = JSONObject.parseObject(rspData);
        String signData = null;
        String data = null;
        try {
            signData = jsonObject.getString("signData");
            String encData = jsonObject.getString("encData");
            LOG.info("收到加密原文:" + encData);
            data = HseEncAndDecUtil.sm4Decrypt(appID,secret,encData);
            LOG.info("解密原文:" + data);
        } catch (Exception e) {
            throw new MipServerException(FuncRetCode.ERROR_CODE_ENCRYPT_ERROR);
        }
        LOG.info("验签回调参数");
        try {
            boolean verifyre = HseEncAndDecUtil.verify(data,rspData,signData,secret,publicKeys);
            LOG.info("回调参数签名校验:" + true);
            if (!verifyre) {
                throw new MipServerException(FuncRetCode.ERROR_CODE_SIGN_ERROR);
            }
        } catch (MipServerException e) {
            throw new MipServerException(FuncRetCode.ERROR_CODE_SIGN_ERROR);
        }
        JSONObject objData = JSONObject.parseObject(data);
        jsonObject.fluentRemove("encData");
        jsonObject.put("data",objData);
        rspData = JSONObject.toJSONString(jsonObject,true);
        LOG.info("解密完整报文:" + rspData);
        return rspData;
    }

    /**
     * 解密验签HOSResultEncDTO
     * @param encDTO
     * @return
     */
    public String processRspData(HOSResultEncDTO encDTO) {
        String rspData = JSONObject.toJSONString(encDTO,true);
        LOG.info("返回报文:" + rspData);
        JSONObject jsonObject = JSONObject.parseObject(rspData);
        Integer codeObj = (Integer)jsonObject.get("code");
        if (codeObj != null && codeObj == 0) {
            String signData = null;
            String data = null;
            try {
                signData = jsonObject.getString("signData");
                String encData = jsonObject.getString("encData");
                LOG.info("返回加密原文:" + encData);
                data = HseEncAndDecUtil.sm4Decrypt(appID,secret,encData);
                LOG.info("返回解密原文:" + data);
            } catch (Exception e) {
                throw new MipServerException(FuncRetCode.ERROR_CODE_ENCRYPT_ERROR);
            }
            try {
                boolean verifyre = HseEncAndDecUtil.verify(data,rspData,signData,secret,publicKeys);
                LOG.info("签名校验:" + verifyre);
                if (!verifyre) {
                    throw new MipServerException(FuncRetCode.ERROR_CODE_SIGN_ERROR);
                }
            } catch (MipServerException e) {
                throw new MipServerException(FuncRetCode.ERROR_CODE_SIGN_ERROR);
            }
            JSONObject objData = JSONObject.parseObject(data);
            jsonObject.fluentRemove("encData");
            jsonObject.put("data",objData);
            rspData = JSONObject.toJSONString(jsonObject,true);
        }
        LOG.info("解密完整报文:" + rspData);
        return rspData;
    }

    public HOSResultEncDTO invoke(HOSParamEncDTO encDTO, String api_url) {
        //String api_url = gsPublicPathService.getApiUrl(infno_code);
        String encryptData = JSONObject.toJSONString(encDTO);
        String rspData = null;
        try {
            LOG.info("请求参数:" + encryptData);
            rspData = HttpUtil.postData(api_url, encryptData);
            LOG.info("请求结果:" + rspData);
        } catch (IOException e) {
            throw new MipServerException(1005,"执行invoke方法失败:" + e.getMessage());
        }
        HOSResultEncDTO resultEncDTO = JSONObject.parseObject(rspData,HOSResultEncDTO.class);
        return resultEncDTO;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy