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"
  1. Rearrange all parameters alphabetically, excluding the parameter sign.

  2. Concatenate the rearranged parameters with &, e.g. aa=hello&xx=1001. (yy is excluded here due to its empty value)

  3. Append SecurityKey to the end of string aa=hello&xx=1001, and will get the pre-sign string likes: aa=hello&xx=1001&key=abc123

  4. Encrypt the pre-sign string by using SHA256 algorithm.

  5. Convert the ciphertext into lower case, and now the string is the sign.

Note: Please keep in mind that the provided example assumes a simplified scenario. In a real implementation, you would need to handle encoding, proper encryption, and other security considerations.

Code Examples

Note: Please don't use function http_build_query to build the parameters.


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