Day6 hashlib module and day6hashlib Module

Source: Internet
Author: User
Tags hmac sha1

Day6 hashlib module and day6hashlib Module

Hashlib Module

Used for file operations related to encryption. 3. X replaces the md5 module and sha module. It mainly provides SHA1, shaloud, SHA256, SHA384, SHA512, and MD5 algorithms.

_ Always_supported = ('md5', 'sha1', 'shawei', 'sha256 ', 'sha384', 'sha512 ')

Import hashlibm = hashlib. md5 () m. update (B "Hello") m. update (B "It's me") print (m. digest () m. update (B "It's been a long time since last time we... ") print (m. digest () # binary format hashprint (m. hexdigest () # hexadecimal hash ''' def digest (self, * args, ** kwargs ): # real signature unknown "" Return the digest value as a string of binary data. "passdef hexdigest (self, * args, ** kwargs): # real signature unknown" "Return the digest value as a string of hexadecimal digits. "pass ''' hash = hashlib. sha512 () hash. update (B 'admin') print (hash. hexdigest ())

 

The above code process is the encryption process. We can use many methods for encryption. First, make sure you use that method for encryption and use update () for encryption.

Not enough? Python also has an hmac module, which internally processes and encrypts the key and content.

HMAC is a Message Authentication mechanism based on the Message Authentication Code MAC. When HMAC is used, both parties of the message communication can identify the authenticity of the message by verifying the authentication key K added to the message;

It is generally used for message encryption in network communication, provided that both parties have to agree on the key first, just like the connector password, and then the message is sent to encrypt the message with the key, the receiver uses key + message plaintext for re-encryption, and checks whether the encrypted value is equal to the relative ratio of the sender. In this way, the authenticity of the message and the legality of the sender can be verified.

>>> Import hmac
>>> H = hmac. new (B "pipei", B "alex ")
>>> H. hexdigest ()
'3c7ffaf55fc7a53f678debb2adca855f'
>>> H = hmac. new (B "pipei ")
>>> H. update (B "alex ")
>>> H. hexdigest ()
'3c7ffaf55fc7a53f678debb2adca855f'

 

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: 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.