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:
The request body is sorted alphabetically and encoded in BASE64.
Concatenate the encoded request body and the secret_key (provided separately).
By using the SHA256 hash function, hash the concatenation result.
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)
package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
)
//Тело запроса меняется в зависимости от запроса
type Request struct {
Agent string `json:"agent"`
Project string `json:"project"`
ServiceCode string `json:"service_code"`
}
//Для примера secret 12345
const secret = "12345"
func main() {
request := Request{
Agent: "tarlan",
Project: "mobile",
ServiceCode: "101",
}
notSortedJson, err := json.Marshal(&request)
if err != nil {
panic(err)
}
var notSortedMap map[string]interface{}
if err = json.Unmarshal(notSortedJson, ¬SortedMap); err != nil {
panic(err)
}
sortedJson, err := json.Marshal(¬SortedMap)
if err != nil {
panic(err)
}
signData := base64.StdEncoding.EncodeToString(sortedJson)
sign := sha256.Sum256([]byte(signData + secret))
fmt.Printf("Sign: %x", sign) }
Last updated