package com.nova.sankuai.domain.api.zhongan;
|
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
import com.alibaba.fastjson.JSONObject;
|
import org.apache.commons.lang.time.DateFormatUtils;
|
|
/**
|
* <p>
|
* description
|
* </p>
|
*
|
* @author denglb 2021/11/15
|
*/
|
public class StandardSignTest {
|
|
private static final String API_NAME = "apiName";
|
private static final String CHANNEL_NO = "channelNo";
|
private static final String SUPPLIER_NO = "supplierNo";
|
private static final String REQ_DATE = "reqDate";
|
private static final String REQ_NO = "reqNo";
|
private static final String RESP_CODE = "respCode";
|
private static final String RESP_MSG = "respMsg";
|
private static final String RESP_NO = "respNo";
|
private static final String RESULT = "result";
|
private static final String THIRD_USER_NO = "thirdUserNo";
|
private static final String APPKEY = "appKey";
|
private static final String SERVICE_NAME = "serviceName";
|
private static final String BIZ_CONTENT = "bizContent";
|
private static final String TIMESTAMP = "timestamp";
|
private static final String FORMAT = "format";
|
private static final String SIGN_TYPE = "signType";
|
private static final String CHARSET = "charset";
|
private static final String VERSION = "version";
|
private static final String SIGN = "sign";
|
private static final String CERTI_NO = "certiNo";
|
private static final String PHONE = "phone";
|
private static final String IS_ALLOW_CREDIT = "isAllowCredit";
|
private static final String ERROR_CODE = "errorCode";
|
private static final String ERROR_MSG = "errorMsg";
|
private static final String VAL_APP_KEY = "Iy3HB0gFFdha2ifV";
|
private static final String VAL_CHANNEL_NO = "DL-ZAXD001";
|
private static final String VAL_CHARSET_DEFAULT = "UTF-8";
|
private static final String VAL_FORMAT_DEFAULT = "json";
|
private static final String VAL_SIGN_TYPE_DEFAULT = "RSA";
|
private static final String VAL_SERVICE_NAME = "duolai.preloan";
|
private static final String VAL_SUCCESS_CODE = "0000";
|
private static final String VAL_SUCCESS_MSG = "";
|
private static final String VAL_VERSION = "1.0.0";
|
//TODO 供应商私钥
|
private String supplierPrivateKey = "";
|
//TODO 供应商公钥
|
private String supplierPublicKey = "";
|
private String zaPrivateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO8h8JCJAMb1nd0uBkzZuWyNnL+atBzJKvIG7escD45ODf0AWKr8vSqLZ01HD86+a496CGjsae6GybK8C1MqiMSwaAsIv31nKD6U8xF607MPrD3r2lyjwUnmqBZY++R6yFNYz9ZDXcdiwCudESRsXunPJq7zfnnglCtEH+qqW8/VAgMBAAECgYEAnVc2gtMyKLbZTPuId65WG7+9oDB5S+ttD1xR1P1cmuRuvcYpkS/Eg6a/rJASLZULDpdbyzWqqaAUPD8QMINvAr3ZtkbwH5R0F/4aqOlx/5B0Okjsp3eSK2bQ8J2m/MmFKZxr6Aily7YUDdxcGcjLizsGi1KDkWS22JRufEeUNA0CQQD+g1XJ7ELqmUtrS4m4XnadB25f0g5QC0tMjoa3d9soMzK3q+Drkv8EZVpGSmSHEo1VlE7HUcnKNvK1BO5Nm4iXAkEA8IeZxaWmEcsRqiuFz8xmYGtKcYTmHgheGF4D+fnnFozSNP+3sS1lfgFQrjUkqUyZOoG1hPc6SDhGS4nbXwiscwJASO+gPR58yrgledkK3ZAMk9GWWtVajqu953GMv7UUU//gD+yspzXX6Q2WgkA9cMvrPtQig1I37sAya5e/JvRkfwJARzzCDEmdP9PW7YFqZjrxb0kXiTuFNAviYnEl2FltWb5nW48JBo6dao5VKONQclvfXfagnjriphUUrLatpB3bhQJAKRfJS6jDAIVKt7So5HOdzk4ipxgrMjG/QtZ1grO+VQectk4+tCwdJhOrr5blvdPQvFVqXBQfXuE7cibZrGs4sQ==";
|
private String zaPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvIfCQiQDG9Z3dLgZM2blsjZy/mrQcySryBu3rHA+OTg39AFiq/L0qi2dNRw/OvmuPegho7GnuhsmyvAtTKojEsGgLCL99Zyg+lPMRetOzD6w969pco8FJ5qgWWPvkeshTWM/WQ13HYsArnREkbF7pzyau83554JQrRB/qqlvP1QIDAQAB";
|
|
/**
|
* 众安侧发起请求
|
*/
|
public void requestSignTest() {
|
Map<String, Object> map = new HashMap<>();
|
map.put(API_NAME, "queryBankList");
|
map.put(CHANNEL_NO, VAL_CHANNEL_NO);
|
map.put(REQ_DATE, new Date());
|
map.put(REQ_NO, "UUID1234567890");
|
map.put(THIRD_USER_NO, "DLJD_DL-ZAXD001_162307848013107");
|
System.out.println("加密前的数据:" + JSONObject.toJSONString(map));
|
//使用供应商的公钥加密
|
String bizContent = SignUtil.encryptByPublicKeyWithNoOrderForLongData(JSONObject.toJSONString(map),
|
supplierPublicKey);
|
Map<String, Object> req = new HashMap<>();
|
req.put(APPKEY, VAL_APP_KEY);// appKey
|
req.put(SERVICE_NAME, VAL_SERVICE_NAME);// serviceName
|
req.put(BIZ_CONTENT, bizContent);// 业务数据
|
req.put(TIMESTAMP, DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"));// 时间戳
|
req.put(FORMAT, VAL_FORMAT_DEFAULT);// 格式化类型
|
req.put(SIGN_TYPE, VAL_SIGN_TYPE_DEFAULT);// 签名类型
|
req.put(CHARSET, VAL_CHARSET_DEFAULT);// 字节编码
|
req.put(VERSION, VAL_VERSION);// API版本号
|
// 众安的私钥加签
|
String sign = SignUtil.doSign(req, zaPrivateKey);
|
// sign签名当成请求入参
|
req.put(SIGN, sign);
|
|
System.out.println("加密后的数据:" + JSONObject.toJSONString(req));
|
|
//----------------------分割线--------------------------
|
|
String getSign = String.valueOf(req.remove(SIGN));
|
// 使用众安的公钥验签
|
boolean verify = SignUtil.rsa256CheckContent(req, getSign, zaPublicKey);
|
// Assert.assertTrue("验签失败......", verify);
|
// 使用供应商的私钥解密
|
String decryptBizContent = SignUtil.decryptRSAByPrivateKeyForLongData(String.valueOf(req.get(BIZ_CONTENT)),
|
supplierPrivateKey);
|
System.out.println("解密后的数据:" + decryptBizContent);
|
}
|
|
/**
|
* 供应商返回
|
*/
|
public void responseSignTest() {
|
Map<String, Object> result = new HashMap<>();
|
result.put(CERTI_NO, "MD5_CERIT_NO");
|
result.put(PHONE, "MD5_PHONE_NO");
|
result.put(IS_ALLOW_CREDIT, true);
|
|
Map<String, Object> respInner = new HashMap<>();
|
respInner.put(RESULT, result);
|
respInner.put(RESP_CODE, "0000");
|
respInner.put(RESP_MSG, "业务成功");
|
respInner.put(RESP_NO, "UUID1234567890");
|
respInner.put(REQ_NO, "UUID1234567890");
|
|
System.out.println("加密前的数据:" + JSONObject.toJSONString(respInner));
|
//使用众安的公钥加密
|
String bizContent = SignUtil.encryptByPublicKeyWithNoOrderForLongData(JSONObject.toJSONString(respInner),
|
zaPublicKey);
|
Map<String, Object> resp = new HashMap<>();
|
resp.put(BIZ_CONTENT, bizContent);// 业务数据
|
resp.put(CHARSET, VAL_CHARSET_DEFAULT);// 字节编码
|
resp.put(FORMAT, VAL_FORMAT_DEFAULT);// 格式化类型
|
resp.put(TIMESTAMP, DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"));// 时间戳
|
resp.put(SIGN_TYPE, VAL_SIGN_TYPE_DEFAULT);// 签名类型
|
resp.put(ERROR_CODE, VAL_SUCCESS_CODE);// 错误码
|
resp.put(ERROR_MSG, VAL_SUCCESS_MSG);// 错误信息
|
// 供应商的私钥加签
|
String sign = SignUtil.doSign(resp, supplierPrivateKey);
|
// sign签名当成请求入参
|
resp.put(SIGN, sign);
|
System.out.println("加密后的数据:" + JSONObject.toJSONString(resp));
|
|
//----------------------分割线--------------------------
|
|
String getSign = String.valueOf(resp.remove(SIGN));
|
// 使用供应商的公钥验签
|
boolean verify = SignUtil.rsa256CheckContent(resp, getSign, supplierPublicKey);
|
// Assert.assertTrue("验签失败......", verify);
|
// 使用众安私钥解密
|
String decryptBizContent = SignUtil.decryptRSAByPrivateKeyForLongData(String.valueOf(resp.get(BIZ_CONTENT)),
|
zaPrivateKey);
|
System.out.println("解密后的数据:" + decryptBizContent);
|
}
|
|
/**
|
* 供应商发起请求
|
*/
|
public void callSignTest() {
|
Map<String, Object> map = new HashMap<>();
|
map.put(CHANNEL_NO, VAL_CHANNEL_NO);
|
map.put(REQ_DATE, new Date());
|
map.put(REQ_NO, "UUID1234567890");
|
map.put(THIRD_USER_NO, "DLJD_DL-ZAXD001_162307848013107");
|
System.out.println("加密前的数据:" + JSONObject.toJSONString(map));
|
//使用众安的公钥加密
|
String bizContent = SignUtil.encryptByPublicKeyWithNoOrderForLongData(JSONObject.toJSONString(map),
|
zaPublicKey);
|
Map<String, Object> req = new HashMap<>();
|
req.put(APPKEY, VAL_APP_KEY);// appKey
|
req.put(SERVICE_NAME, VAL_SERVICE_NAME);// serviceName
|
req.put(BIZ_CONTENT, bizContent);// 业务数据
|
req.put(TIMESTAMP, DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"));// 时间戳
|
req.put(FORMAT, VAL_FORMAT_DEFAULT);// 格式化类型
|
req.put(SIGN_TYPE, VAL_SIGN_TYPE_DEFAULT);// 签名类型
|
req.put(CHARSET, VAL_CHARSET_DEFAULT);// 字节编码
|
req.put(VERSION, VAL_VERSION);// API版本号
|
req.put(SUPPLIER_NO, "供应商编码");//TODO 供应商发起请求该值必填,值为众安侧分配的编码
|
// 供应商的私钥加签
|
String sign = SignUtil.doSign(req, supplierPrivateKey);
|
// sign签名当成请求入参
|
req.put(SIGN, sign);
|
System.out.println("加密后的数据:" + JSONObject.toJSONString(req));
|
|
//----------------------分割线--------------------------
|
|
String getSign = String.valueOf(req.remove(SIGN));
|
// 使用供应商的公钥验签
|
boolean verify = SignUtil.rsa256CheckContent(req, getSign, supplierPublicKey);
|
// Assert.assertTrue("验签失败......", verify);
|
// 使用众安私钥解密
|
String decryptBizContent = SignUtil.decryptRSAByPrivateKeyForLongData(String.valueOf(req.get(BIZ_CONTENT)),
|
zaPrivateKey);
|
System.out.println("解密后的数据:" + decryptBizContent);
|
}
|
|
/**
|
* 众安侧返回
|
*/
|
public void callResponseSignTest() {
|
Map<String, Object> map = new HashMap<>();
|
map.put(CERTI_NO, "MD5_CERIT_NO");
|
map.put(PHONE, "MD5_PHONE_NO");
|
map.put(IS_ALLOW_CREDIT, true);
|
System.out.println("加密前的数据:" + JSONObject.toJSONString(map));
|
//使用供应商的公钥加密
|
String bizContent = SignUtil.encryptByPublicKeyWithNoOrderForLongData(JSONObject.toJSONString(map),
|
supplierPublicKey);
|
Map<String, Object> resp = new HashMap<>();
|
resp.put(BIZ_CONTENT, bizContent);// 业务数据
|
resp.put(CHARSET, VAL_CHARSET_DEFAULT);// 字节编码
|
resp.put(FORMAT, VAL_FORMAT_DEFAULT);// 格式化类型
|
resp.put(TIMESTAMP, DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS"));// 时间戳
|
resp.put(SIGN_TYPE, VAL_SIGN_TYPE_DEFAULT);// 签名类型
|
resp.put(ERROR_CODE, VAL_SUCCESS_CODE);// 错误码
|
resp.put(ERROR_MSG, VAL_SUCCESS_MSG);// 错误信息
|
// 众安的私钥加签
|
String sign = SignUtil.doSign(resp, zaPrivateKey);
|
// sign签名当成请求入参
|
resp.put(SIGN, sign);
|
|
System.out.println("加密后的数据:" + JSONObject.toJSONString(resp));
|
|
//----------------------分割线--------------------------
|
|
String getSign = String.valueOf(resp.remove(SIGN));
|
// 使用众安的公钥验签
|
boolean verify = SignUtil.rsa256CheckContent(resp, getSign, zaPublicKey);
|
// Assert.assertTrue("验签失败......", verify);
|
// 使用供应商的私钥解密
|
String decryptBizContent = SignUtil.decryptRSAByPrivateKeyForLongData(String.valueOf(resp.get(BIZ_CONTENT)),
|
supplierPrivateKey);
|
System.out.println("解密后的数据:" + decryptBizContent);
|
}
|
}
|