MD5签名
# MD5签名
# MD5签名示例
假设 appSecret 为 “your_app_secret”,param1、param2表示为每个接口中的“公共参数”:
| 参数名 | 参数值 |
|---|---|
| param1 | value1 |
| param2 | value2 |
| appSecret | your_app_secret |
1.将 appSecret 加入到“公共参数”中 :已经包含在上述参数列表里了。
2.对参数的 key 进行字典升序排列 :排好序后的参数名为 “appSecret”“param1”“param2”。
3.去除首尾空格(假设参数值本身没有首尾空格)后,拼接为 “appSecretyour_app_secretparam1value1param2value2”
4.使用 MD5 算法对拼接后的字符串进行计算,得到的值就是签名参数 sign 的值。
注意:
1.请勿将密钥参与到http中传递,它是双方保管的密钥,一旦泄露,请申请更换。
2.东福在验签时,地址栏中除了sign参数之外,所有参数都会参与签名计算,因此地址栏参数传递需要参与签名计算,避免签名错误。
# java代码示例
public class SignUtils {
public static String sign(Map<String, String> params) throws NoSuchAlgorithmException {
StringBuilder StringBuilder = new StringBuilder();
List<String> keyList = params.keySet().stream().sorted().collect(Collectors.toList());
for (String s : keyList) {
StringBuilder.append(s).append(params.get(s));
}
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(StringBuilder.toString().getBytes(StandardCharsets.UTF_8));
byte[] encryContext = md.digest();
int i;
StringBuilder buf = new StringBuilder();
for (byte b : encryContext) {
i = b;
if (i < 0) i += 256;
if (i < 16) buf.append("0");
buf.append(Integer.toHexString(i));
}
return buf.toString();
}
@SneakyThrows
public static void main(String[] args) {
LinkedHashMap<String, String> params = new LinkedHashMap<>();
params.put("appId", "abc123");
params.put("timestamp", "1597300776947");
params.put("account", "zhangsan");
params.put("appSecret", "1f44269c50674f2a85135d8bd91dd37c");
System.out.println(sign(params));
// 输出:6b6a461524d8931bb4b38ee283640474
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
← 11.6 运动圈排行榜 错误码→