Quickstart
Signature
Quickstart
Balance
Quickstart
Signature
Signature Description
1. Assemble Parameters:
Assume all received data is represented as a set M. Extract the non-empty values from set M, and sort the parameters by their names in ascending ASCII order (lexicographical order). Concatenate these parameters into a string str in URL key-value pair format as follows:
"key1=value1&key2=value2&key3=value3..."
Pay close attention to the following rules:
Parameter names must be sorted in ascending ASCII order (lexicographical order).
Parameter names are case-sensitive.
2. Append the Merchant Key:
Append the merchant key to the end of str, and perform an MD5 hash operation on str with the merchant key to obtain the sign value.
PHP code
function sign($data, $secret){
ksort($data);
$param = [];
foreach ($data as $k => $v) {
array_push($param, $k . '=' . $v);
}
$queryString = implode('&', $param);
return md5($queryString . $secret);
}
JAVA code
public static String sign(Map<String, Object> data, String secret) {
SortedMap<String, Object> sortedData = new TreeMap<>(data);
StringBuilder param = new StringBuilder();
for (Map.Entry<String, Object> entry : sortedData.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
String valueStr = value instanceof Integer ? String.valueOf(value) : value.toString();
if (param.length() > 0) {
param.append("&");
}
param.append(key).append("=").append(valueStr);
}
String queryString = param.toString() + secret;
return md5(queryString);
}
private static String md5(String input) {
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(input.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte b : array) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (Exception e) {
throw new RuntimeException("MD5 calculation error", e);
}
}
On this page