Signature Code Example
To further clarify the new signature rules, we provide demos of the signature algorithm in three languages: Python, Go, and PHP.
The input parameter payload
is a dictionary containing the data to be signed, representing the first layer of parameter-value pairs in the request JSON. Ideally, it should only include the key-value pairs that participate in the calculation, but the program also incorporates filtering.
Please adjust the other parameters according to your specific requirements.
Python
import json
import time
import hashlib
import requests
def create_headers(payload):
"""
Create required values for signature verification
:param payload: Dictionary of data to be signed, which should not contain lists,
dictionaries, or strings longer than 1024
:return:
"""
sid = 123456
secret = 'XXXXXX'
time_stamp = int(time.time())
# Filter out lists, dictionaries, or overly long strings and values
filtered_data = {k: v for k, v in payload.items() if not isinstance(v, (list, dict)) and len(str(v)) <= 1024}
filtered_data['sid'] = sid
filtered_data['timeStamp'] = time_stamp
sorted_items = sorted(filtered_data.items()) # Sort the dictionary items
sign_string = "&".join(f"{key}={value}" for key, value in sorted_items) # Build the string for signing
# Append the signing secret
sign_string += "&key={}".format(secret)
sign = hashlib.md5(sign_string.encode('utf-8')).hexdigest()
headers = {
'X-EEO-SIGN': sign,
'X-EEO-UID': f'{sid}',
'X-EEO-TS': f'{time_stamp}',
'Content-Type': 'application/json'
}
print(f'headers: {headers}')
return headers
import (
"crypto/md5"
"encoding/hex"
"fmt"
"sort"
"strconv"
"strings"
"time"
)
func createHeaders(payload map[string]interface{}) map[string]string {
sid := 123456
secret := "XXXXXX"
timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
// Filter out arrays, objects, or overly long strings
var filteredData []string
for k, v := range payload {
switch v.(type) {
case []interface{}, map[string]interface{}, string:
if str, ok := v.(string); ok && len(str) <= 1024 {
filteredData = append(filteredData, fmt.Sprintf("%s=%v", k, v))
}
default:
if num, ok := v.(int); ok {
filteredData = append(filteredData, fmt.Sprintf("%s=%d", k, num))
}
}
}
// Add sid and timeStamp
filteredData = append(filteredData, "sid="+strconv.Itoa(sid))
filteredData = append(filteredData, "timeStamp="+timeStamp)
// Sort the dictionary items
sort.Strings(filteredData)
// Construct the string for signing
signString := strings.Join(filteredData, "&")
signString += "&key=" + secret
fmt.Printf("signString: %s\n", signString)
// Generate the signature using the MD5 algorithm
hash := md5.Sum([]byte(signString))
sign := hex.EncodeToString(hash[:])
headers := map[string]string{
"X-EEO-SIGN": sign,
"X-EEO-UID": strconv.Itoa(sid),
"X-EEO-TS": timeStamp,
"Content-Type": "application/json",
}
return headers
}
PHP
<?php
function createHeaders($payload) {
$sid = 123456;
$secret = 'XXXXXX';
$timeStamp = time();
// Filter out arrays, objects, or overly long strings
$filteredData = array_filter($payload, function ($value) {
return !is_array($value) && !is_object($value) && strlen($value) <= 1024;
});
// Add sid and timeStamp to the filtered data
$filteredData['sid'] = $sid;
$filteredData['timeStamp'] = $timeStamp;
// Sort the array items by key
ksort($filteredData);
// Build the string for signing
$signString = http_build_query($filteredData) . "&key=" . $secret;
// Calculate the signature
$sign = md5($signString);
$headers = array(
'X-EEO-SIGN: ' . $sign,
'X-EEO-UID: ' . $sid,
'X-EEO-TS: ' . $timeStamp,
'Content-Type: application/json'
);
// Output the headers; you can remove this in actual usage
echo 'headers: '; print_r($headers);
return $headers;
}
?>