The signature generation method is as follows:
-
Sort all request parameters (excluding signature parameter) in ascending order of parameter name ASCII code table. For example: foo=1, bar=2, foo_bar=3, baz=4 The sorted order is bar=2, baz=4, foo=1, foobar=3.
-
Construct the sorted parameter names and parameter values into strings in the format: key1+value1+key2+value2… . The result of the construction according to the above example is: bar2baz4foo1foobar3.
-
Select the secretKey paired with secretId and add it to the parameter string constructed in the previous step, such as secretKey=6308afb129ea00301bd7c79621d07591, then the final parameter string is bar2baz4foo1foobar36308afb129ea00301bd7c79621d07591.
-
Encode the string assembled in the previous step in utf-8, use the MD5 algorithm to digest the string, calculate the signature parameter value (lowercase), and add it to the interface request parameter. MD5 is a digest algorithm with a length of 128 bits, expressed in hexadecimal, and a hexadecimal character can represent 4 units, so the length of the signed string is fixed to 32 hexadecimal characters.
Signature generation sample code:
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
// Note: You can choose appropriate auxiliary tools according to the actual situation of your project.
// depend Apache Commons Codec(commons-codec: commons-codec)
import org.apache.commons.codec.digest.DigestUtils;
// depend Apache Commons Lang(org.apache.commons: commons-lang3)
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
/**
* Generate signature information
* @param secretKey Product private key
* @param params Interface request parameter name and parameter value map, excluding signature parameter name
*/
public static String genSignature(String secretKey, Map<String, String> params) {
String[] paramNames = params.keySet().toArray(new String[0]);
Arrays.sort(paramNames);
StringBuilder sb = new StringBuilder();
for (String name : paramNames) {
String value = ObjectUtils.defaultIfNull(params.get(name), StringUtils.EMPTY);
sb.append(name).append(value);
}
sb.append(secretKey);
return DigestUtils.md5Hex(sb.toString().getBytes(StandardCharsets.UTF_8));
}
using System;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Linq;
// Note: Typically, you would create a Dictionary object as paramDict
public static string GenSignature(string secretKey, IEnumerable<KeyValuePair<string, string>> paramDict)
{
StringBuilder sb = new StringBuilder();
foreach (var param in paramDict.OrderBy(p => p.Key, StringComparer.Ordinal)) {
var name = param.Key;
var value = param.Value ?? String.Empty;
sb.Append(name).Append(value);
}
sb.Append(secretKey);
using (MD5 md5 = MD5.Create())
{
var md5Bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
return String.Concat(md5Bytes.Select(c => c.ToString("x2")));
}
}
// Note: You can choose appropriate auxiliary tools according to the actual situation of your project.
const crypto = require('crypto');
var genSignature = function (secretKey, paramDict) {
var sortedNames = Object.keys(paramDict).sort();
var paramStr = '';
for (var i=0; i<sortedNames.length; i++) {
var name = sortedNames[i];
paramStr += name + (paramDict[name] || '');
}
paramStr += secretKey;
return crypto.createHash('md5')
.update(paramStr, "UTF-8")
.digest('hex');
};
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
""" Generate signature information
Args:
secret_key Product private key
param_dict API request parameters, excluding the signature parameter
"""
def gen_signature(secret_key, param_dict):
param_str = ''.join([
str(name) + str(param_dict[name] or '')
for name
in sorted(param_dict.keys())
])
param_str += secret_key
return hashlib.md5(param_str.encode("utf-8")).hexdigest()
/**
* Generate signature information
* $secret_key Product private key
* $params param_dict API request parameters, excluding the signature parameter
*/
function gen_signature($secret_key, $params) {
ksort($params);
$tmp_array = array_map(
function ($value, $name) { return $name . ($value ?: ''); },
$params,
array_keys($params));
$tmp_str = implode($tmp_array);
$tmp_str .= $secret_key;
return md5(mb_convert_encoding($tmp_str, "UTF-8"));
}