You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
6.3 KiB

11 months ago
package com.charge.util;
11 months ago
import com.alibaba.fastjson.JSONArray;
11 months ago
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
11 months ago
11 months ago
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
11 months ago
import java.util.ArrayList;
11 months ago
import java.util.Map;
import java.util.TreeMap;
11 months ago
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import javax.servlet.ServletOutputStream;
11 months ago
/**
*
*
* @author keytop
* @date 2020/3/6
*/
public class SignUtils {
11 months ago
/**
* JSON
*
* @param str
* @return
*/
public static boolean isJsonString(String str) {
if (str.indexOf("{") >= 0) return true;
if (str.indexOf("[") >= 0) return true;
return false;
}
11 months ago
/**
*
*
* {"amount":100,"orderNo":"闽C12345","payTime":"2020-03-06 10:57:22","freeDetail":"[{\"code\":\"\",\"money\":100,\"time\":0,\"type\":0}]","paySource":"85d15350778b11e9bbaa506b4b2f6421","outOrderNo":"T20200306124536001","parkId":"1000001","payableAmount":200,"reqId":"5be4e3e6d5704a7d91ccbd9731d970f5","payType":1006,"payMethod":6,"appId":"85d15350778b11e9bbaa506b4b2f6421","freeTime":0,"paymentExt":"{\"deviceNo\":\"123456\"}","freeMoney":100,"ts":1583744086841}
* urlamount=100&freeDetail=[{"code":"","money":100,"time":0,"type":0}]&freeMoney=100&freeTime=0&orderNo=C12345&outOrderNo=T20200306124536001&parkId=1000001&payMethod=6&paySource=85d15350778b11e9bbaa506b4b2f6421&payTime=2020-03-06 10:57:22&payType=1006&payableAmount=200&paymentExt={"deviceNo":"123456"}&reqId=5be4e3e6d5704a7d91ccbd9731d970f5&ts=1583744086841&EED96C219E83450A
* B19F7863ADCC8B5442A757AC7B90F6AC
*
* @param requestBody
* @param appSecret
* @return
*/
public static String paramsSign(JSONObject requestBody, String appSecret) {
TreeMap<String, String> params = new TreeMap<>();
11 months ago
for (String key : new ArrayList<>(requestBody.keySet())) {
// 假设我们要移除以 "age" 开头的属性
Object value = requestBody.get(key);
if (isJsonString(value.toString())) {
requestBody.remove(key);
}
}
11 months ago
//过滤掉keyappId字段空属性及Map或List等复杂对象
requestBody.entrySet().stream().filter(
p -> !"key".equals(p.getKey())
&& !"appId".equals(p.getKey())
&& p.getValue() != null
&& !(p.getValue() instanceof Map)
&& !(p.getValue() instanceof Iterable))
.forEach(p -> {
if (!p.getValue().equals("")) {
params.put(p.getKey(), p.getValue().toString());
}
});
//拼接appSecret
String temp = Joiner.on("&").withKeyValueSeparator("=").join(params).concat("&").concat(appSecret);
11 months ago
String MD5 = md5(temp).toUpperCase();
return MD5;
11 months ago
}
11 months ago
/**
*
*
* @param requestBody
* @param appSecret
* @return
*/
public static String paramsAnKuaiSign(JSONObject requestBody, String appSecret) {
TreeMap<String, String> params = new TreeMap<>();
11 months ago
11 months ago
requestBody.entrySet().stream().filter(
p -> p.getValue() != null
&& !(p.getValue() instanceof Map)
11 months ago
&& !(p.getValue() instanceof Iterable)
&& !(p.getValue() instanceof JSONObject)
&& !(p.getValue() instanceof JSONArray)
)
11 months ago
.forEach(p -> {
if (!p.getValue().equals("")) {
params.put(p.getKey(), p.getValue().toString());
}
});
//拼接appSecret
String temp = Joiner.on("&").withKeyValueSeparator("=").join(params).concat("&secret=").concat(appSecret);
String res = md5(temp);
return res;
}
11 months ago
/**
* md5 , mysql,JavaScriptmd5.
*
* @param plainText
* @return
*/
private static String md5(String plainText) {
if (null == plainText) {
plainText = "";
}
String mD5Str = null;
try {
// JDK 支持以下6种消息摘要算法不区分大小写
// md5,sha(sha-1),md2,sha-256,sha-384,sha-512
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes(StandardCharsets.UTF_8));
byte[] b = md.digest();
int i;
StringBuilder builder = new StringBuilder(32);
for (byte value : b) {
i = value;
if (i < 0) {
i += 256;
}
if (i < 16) {
builder.append("0");
}
builder.append(Integer.toHexString(i));
}
mD5Str = builder.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return mD5Str;
}
11 months ago
public static void main(String[] args) {
String source = "{\"amount\":100,\"orderNo\":\"闽C12345\",\"payTime\":\"2020-03-06 10:57:22\",\"freeDetail\":\"[{\\\"code\\\":\\\"\\\",\\\"money\\\":100,\\\"time\\\":0,\\\"type\\\":0}]\",\"paySource\":\"EED96C219E83450A\",\"outOrderNo\":\"T20200306124536001\",\"parkId\":\"1000001\",\"payableAmount\":200,\"reqId\":\"748584ae47104b0ab239732767ddc679\",\"payType\":1006,\"payMethod\":6,\"appId\":\"EED96C219E83450A\",\"freeTime\":0,\"paymentExt\":\"{\\\"deviceNo\\\":\\\"123456\\\"}\",\"freeMoney\":100,\"ts\":1583464576264}";
JSONObject jo = JSONObject.parseObject(source);
String appSecret = "85d15350778b11e9bbaa506b4b2f6421";
System.out.println(paramsSign(jo, appSecret));
//4402E3F7DB94CCCF034F9AFA86F5F9CD
//4402E3F7DB94CCCF034F9AFA86F5F9CD
}
11 months ago
}