Objective
Open interface in order to avoid being called by others, wasting server resources, which involves signing (Signature) encryption
The API uses the signature method (Signature) to authenticate the interface (authentication). Each request needs to include the signature information in the request to verify the user's identity.
Interface signature
1. According to the requirements of the document, look at the interface signature rules, each company's signature rules are different, the following is for reference only:
2. As you can see from this document, the following points are involved:
HMAC (hash-based Message authentication Code) is commonly used for interface signature validation
The supported algorithms are MD5, SHA1,sha256, sha512, Adler32, CRC32, etc.
Convert binary to HEX
Convert a string to lowercase
MD5 encryption of the POST request body
Timestamp in UNIX format, unit s
MD5 encryption
1 1.md5 encryption is relatively simple, directly call the library built into hashlib can solve
3 import hashlib
4 # MD5 encryption
5 def jiamimd5(src):
6 m = hashlib.md5()
7 m.update(src.encode(‘UTF-8‘))
8 return m.hexdigest()
Timestamp
1. Generate a Unix timestamp, because Python gets a decimal point, the type of int can be
HMAC_SHA256 encryption
1. First use the hmac method to generate the signature string, note that the two parameters passed in new() are the bytes type.
2 import hmac
3 import hashlib
4 appkey = "Need to apply"
5 strToSign = "Generate according to document rules"
6 # hmac_sha256 encryption
7 signature = hmac.new(bytes(appkey, encoding=‘utf-8‘), bytes(strToSign, encoding=‘utf-8‘), digestmod=hashlib.sha256).digest()
8 # print(signature)
9 # binary to HEX
10 HEX = signature.hex()
11 # print(HEX)
12 # Change string to lowercase
13 lowsigne = HEX.lower()
Python interface Automation 23-signature (signature) authentication (authentication) encryption (HEX, MD5, hmac-sha256)