签名生成规则[必须]
Header通用请求参数 [sign]生成算法规则。
签名生成规则如下:
将请求参数按照键名进行字典排序。
遍历排序后的键值对,将键值对连接成字符串,形式为
key1=value1&key2=value2&...
,排除键名为"sign"的键值对。将连接好的字符串与
SecretKey
以及时间戳timestamp
拼接。对拼接好的字符串进行MD5哈希。
将生成的哈希作为请求头中的
sign
字段的值。
其中,SecretKey
为您的密钥,timestamp
为当前时间戳。
var SecretKey = "YOUR_SECRET_KEY";
var timestamp = (new Date()).valueOf();
var param = JSON.parse(request.data);
var newkey = Object.keys(param).sort();
var dataStr = "";
for(var i = 0; i < newkey.length; i++) {
if(newkey[i] == "sign") {
continue;
}
if(param[newkey[i]].length != 0) {
if(dataStr.length == 0) {
dataStr += newkey[i] + "=" + param[newkey[i]];
} else {
dataStr += "&" + newkey[i] + "=" + param[newkey[i]];
}
}
}
var strToHash = SecretKey + dataStr + timestamp;
var hash = CryptoJS.MD5(strToHash).toString();
postman.setGlobalVariable("timestamp", timestamp);
postman.setGlobalVariable("sign", hash);
注意: 请将YOUR_SECRET_KEY
替换为您的实际密钥。
以下是使用 Golang 和 Java 编写签名生成规则的示例:
Golang 示例:
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"sort"
"strconv"
"time"
)
func generateSign(secretKey string, param map[string]string) string {
// Sort keys
var keys []string
for k := range param {
keys = append(keys, k)
}
sort.Strings(keys)
// Concatenate key-value pairs
var concatenatedStr string
for _, k := range keys {
if k == "sign" {
continue
}
if param[k] != "" {
if concatenatedStr == "" {
concatenatedStr += k + "=" + param[k]
} else {
concatenatedStr += "&" + k + "=" + param[k]
}
}
}
// Append timestamp
timestamp := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)
concatenatedStr += timestamp
// Add secret key
concatenatedStr = secretKey + concatenatedStr
// Calculate MD5 hash
hash := md5.Sum([]byte(concatenatedStr))
return hex.EncodeToString(hash[:])
}
func main() {
secretKey := "YOUR_SECRET_KEY"
param := map[string]string{
"param1": "value1",
"param2": "value2",
// Add more parameters as needed
}
sign := generateSign(secretKey, param)
fmt.Println("Generated Sign:", sign)
}
Java 示例:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static String generateSign(String secretKey, Map<String, String> param) {
// Sort parameters by key
Map<String, String> sortedParam = new TreeMap<>(param);
// Concatenate key-value pairs
StringBuilder concatenatedStr = new StringBuilder();
for (Map.Entry<String, String> entry : sortedParam.entrySet()) {
if (!entry.getKey().equals("sign") && !entry.getValue().isEmpty()) {
if (concatenatedStr.length() == 0) {
concatenatedStr.append(entry.getKey()).append("=").append(entry.getValue());
} else {
concatenatedStr.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
}
}
// Append timestamp
String timestamp = String.valueOf(System.currentTimeMillis());
concatenatedStr.append(timestamp);
// Add secret key
concatenatedStr.insert(0, secretKey);
// Calculate MD5 hash
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] hashBytes = md.digest(concatenatedStr.toString().getBytes());
StringBuilder sign = new StringBuilder();
for (byte b : hashBytes) {
sign.append(String.format("%02x", b));
}
return sign.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String secretKey = "YOUR_SECRET_KEY";
Map<String, String> param = new HashMap<>();
param.put("param1", "value1");
param.put("param2", "value2");
// Add more parameters as needed
String sign = generateSign(secretKey, param);
System.out.println("Generated Sign: " + sign);
}
}
注意: 请将 YOUR_SECRET_KEY
替换为您的实际密钥。
Last updated