Creating a Signature

Requests to interact with the service are signed using the SHA256 algorithm. The signature is created separately for each request.

To create a signature, the following steps must be taken:

  1. The request body is sorted alphabetically and encoded in BASE64.

  2. Concatenate the encoded request body and the secret_key (provided separately).

  3. By using the SHA256 hash function, hash the concatenation result.

  4. Add X-signature to the request headers.

Request fields filled with "" do not participate in creating the signature.

import json
import base64
import hashlib
#Тело запроса меняется в зависимости от запроса

request_data = {
  "agent": "tarlan",
  "project": "mobile",
  "service_code": "101",
}
#Для примера взяли secret 12345
secret = "12345"

sorted_data = json.dumps(
      request_data,
      sort_keys=True,
      ensure_ascii=False,
      separators=(',', ':'),
  )


base64_encoded_data = base64.b64encode(sorted_data.encode()).decode()

data_to_sign = base64_encoded_data + secret

sha256_hash = hashlib.sha256(data_to_sign.encode()).hexdigest()

print("Sign:",sha256_hash)

Last updated