请求加签说明
签名方式:RSA
签名算法:SHA1WithRSA
排序拼接方式:按照首字母顺序排序,以key1=value1&key2=value2&key3=value3.....形式组装加签串,value为空字段不参与签名
加签私钥:如果商户还未设置RSA私钥,由商户自己通过公私钥生成器生成,将生成的公私钥(通过专业版平台)上传到商户信息中。
加签代码示例:
public static void main(String[] args) throws Exception {
// 字段按照字母序排序并使用key=value&方式拼接
Map<String, String> paramsMap = new TreeMap<>();
paramsMap.put("openId", "2398724h98y912");
paramsMap.put("appId", "djgeiq978y933");
paramsMap.put("appName", "2324424214124124");
paramsMap.put("mchId", "93745648");
paramsMap.put("outTradeNo", "hiuhei09023j12j40206");
paramsMap.put("timeStart", "20180801160000");
paramsMap.put("timeExpire", "20180801180000");
paramsMap.put("totalFee", "100");
paramsMap.put("notifyUrl", "https://notify.shengpay.com/notify");
paramsMap.put("nonceStr", "pWUVWVYWNQhnTXpQzhIzMGCwqBkZclzg");
paramsMap.put("goodsTag", "WIFI_DISCOUNT_001");
paramsMap.put("body", "抓娃娃机-游戏充值");
paramsMap.put("detail", "{\"goodsDetails\":[{\"goodsId\":\"goodsId1\",\"goodsName\":\"goodsIName1\",\"price\":100,\"quantity\":1}]}");
paramsMap.put("attach", "{\"memo\":\"test\"}");
paramsMap.put("tradeType", "APP");
paramsMap.put("signType", "RSA");
// Joiner 是 guava提供工具类
String origStr = Joiner.on("&").withKeyValueSeparator("=").join(paramsMap);
// 加签算法
String privateKey = "RSA私钥";
byte[] keyBytes = Base64Utils.decode(privateKey);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
Signature signature = Signature.getInstance("SHA1WithRSA");
signature.initSign(privateK);
signature.update(origStr.getBytes());
String sign = new String(Base64.encodeBase64(signature.sign()));
System.out.println(sign);
}
演示demo:
请求JSON:
{
"openId":"2398724h98y912",
"appId":"djgeiq978y933",
"appName":"2324424214124124",
"mchId":"93745648",
"outTradeNo":"hiuhei09023j12j40206",
"timeStart":"20180801160000",
"timeExpire":"20180801180000",
"totalFee":100,
"notifyUrl":"https://notify.shengpay.com/notify",
"nonceStr":"pWUVWVYWNQhnTXpQzhIzMGCwqBkZclzg",
"goodsTag":"WIFI_DISCOUNT_001",
"body":"抓娃娃机-游戏充值",
"detail":"{\"goodsDetails\":[{\"goodsId\":\"goodsId1\",\"goodsName\":\"goodsIName1\",\"price\":100,\"quantity\":1}]}",
"attach":"{\"memo\":\"test\"}",
"tradeType":"APP",
"limitPay":"",
"signType":"RSA",
"sign":"ZdNjNnZL2EMsDvX9C/2ap8uKk18x8ungs42u9y96PrWCQ7flMDePcLL61eG0bgBJdJzd88N54NLzprPa5CSIrI0+FZTWPDDmVMoXz74dyxqFzub9aag0uoyJvD8LDI4bIF6skHuJr6j4tQUPugJ5MtNPJOWG7aHjY4I9HXdN9iw="
}
排序拼接后的加签原串:
appId=djgeiq978y933&appName=2324424214124124&attach={"memo":"test"}&body=抓娃娃机-游戏充值&detail={"goodsDetails":[{"goodsId":"goodsId1","goodsName":"goodsIName1","price":100,"quantity":1}]}&goodsTag=WIFI_DISCOUNT_001&mchId=93745648&nonceStr=pWUVWVYWNQhnTXpQzhIzMGCwqBkZclzg¬ifyUrl=https://notify.shengpay.com/notify&openId=2398724h98y912&outTradeNo=hiuhei09023j12j40206&signType=RSA&timeExpire=20180801180000&timeStart=20180801160000&totalFee=100&tradeType=APP
加签结果:
W7ujnhg4B/wGzVbgMHZrUx0YSkGoIoc8edFOB+Mxdq0h/5M93aa4dyTcutHXcUh73H8PEM4oHBB/LbWegP1J8JlI7dwEz2Wf8/8GWs6OBwdrRmS1BRXJTjjpprbKr3vp5JAEu1XU9cNjav/RaiwL66CNsuFZfgYDLVl6mTC5QYI=