Python interface Automation 23-signature (signature) authentication (authentication) encryption (HEX, MD5, hmac-sha256)

Source: Internet
Author: User
Tags hmac md5 encryption


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-sha256

HMAC (hash-based Message authentication Code) is commonly used for interface signature validation
The supported algorithms are MD5, SHA1,sha256, sha512, Adler32, CRC32, etc.

    • Binary2hex

Convert binary to HEX

    • lowercase

Convert a string to lowercase

    • MD5 (HTTP body)

MD5 encryption of the POST request body

    • Unix time stamp

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() 


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 =, 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.