Signature
Table of Contents
Introduction
In this section, we will show you the mechanism of generating parameter sign
.
Additionally, we will provide some code examples to expedite the integration process.
Mechanism
The signature is hashed with SecurityKey
using SHA256
algorithm.
For example:
curl 'https://domain/path/getSth?xx=1001&yy=&aa=hello&sign=signstring'
curl -X POST 'https://domain/path/updateSth' \
-d '{"xx":1001,"yy":"","aa":"hello","sign":"signstring"}' \
-H "Content-Type:application/json"
Rearrange all parameters alphabetically, excluding the parameter
sign
.Concatenate the rearranged parameters with
&
, e.g.aa=hello&xx=1001
. (yy
is excluded here due to its empty value)Append
SecurityKey
to the end of stringaa=hello&xx=1001
, and will get the pre-sign string likes:aa=hello&xx=1001&key=abc123
Encrypt the pre-sign string by using
SHA256
algorithm.Convert the ciphertext into lower case, and now the string is the
sign
.
Code Examples
function genSignature($params, $securityKey) {
if (!is_array($params) || count($params) == 0) {
return '';
}
ksort($params);
$qs = '';
foreach ($params as $k => $v) {
if (!empty($v)) {
$qs = $qs . "{$k}={$v}&";
}
}
// e.g. $qs='aa=hello&xx=1001&key=abc123'
$qs = $qs.'key='.$securityKey;
return strtolower(hash_hmac("sha256", $qs, $securityKey));
}
// using
$params = [
'aa' => 'hello',
'xx' => 1001,
'yy' => ""
];
$sign = genSignature($params, 'abc123');
// output: 1c4492e23f7812c5781a30046c5d760ba3ae344de99a5700542715866f448825
echo $sign;
Last updated