Hashlib module -- encryption, hashlib module --

Source: Internet
Author: User
Tags hmac

Hashlib module -- encryption, hashlib module --

Used for encryption-related operations. In 3.x, the md5 module and sha module are replaced. The SHA1, sha1_, SHA256, SHA384, SHA512, and MD5 algorithms are mainly provided.

Import hashlibm = hashlib. md5 () m. update (B "hello") print (m. hexdigest () m. update (B "It's me") print (m. hexdigest () m. update (B "It's been a long time since we spoken. ") print (m. hexdigest () m2 = hashlib. md5 () m2.update (B "helloIt's me") print (m2.hexdigest () # output Digest # the second and fourth md5 values are the same, so the second encrypted version is helloIt's sme.

  print(m.digest()) # Binary hash

  print ( len (m.hexdigest()))  # Hexadecimal hash 
# ######## sha1 ######## hash = hashlib.sha1()hash.update('admin')print(hash.hexdigest()) # ######## sha256 ######## hash = hashlib.sha256()hash.update('admin')print(hash.hexdigest())  # ######## sha384 ######## hash = hashlib.sha384()hash.update('admin')print(hash.hexdigest()) # ######## sha512 ######## hash = hashlib.sha512()hash.update('admin')print(hash.hexdigest())

 

 

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 hmach = hmac. new ("Tianwang gaidihu ". encode (encoding = "UTF-8"), "You are a squirrel ". encode (encoding = "UTF-8") print (h. digest () print (h. hexdigest () # output B 'fx \ xad \ xdd \ x9e \ xd6 \ xddcQN \ x82c \ xcd \ xd9 \ x80-'6678addd9ed6dd63514e8263cdd9802d

  

 

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.