The HMAC module that comes with Python

Source: Internet
Author: User
Tags hmac

Python's own HMAC module implements the standard HMAC algorithm

We first need to prepare the original message to be computed, a random key, a hash algorithm, where MD5 is used, and the code for the HMAC is as follows:

Import= b'HelloWorld'= b'secret'  = hmac.new (key,message,digestmod='MD5')print( H.hexdigest ())

It can be seen that using HMAC is very similar to the normal hash algorithm. The length of the HMAC output is the same as the length of the original hash algorithm. Note that the key and message passed in are both bytes types, and the str type needs to be encoded first bytes .

defHmac_md5 (Key, s):returnHmac.new (Key.encode ('Utf-8'), S.encode ('Utf-8'),'MD5'). Hexdigest ()classUser (object):def __init__(self, username, password): Self.username=username Self.key="'. Join ([Chr (Random.randint (48, 122)) forIinchRange (20)]) Self.password= HMAC_MD5 (self.key, password)

The HMAC module that comes with Python

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: info-contact@alibabacloud.com 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.