Python hashlib Module

Source: Internet
Author: User
Tags hmac md5 encryption sha1 sha256 algorithm

Hashlib Module

I. Hashlib module

For cryptographic related operations, the 3.x force replaces the MD5 module and the SHA module, providing

: SHA1,SHA224,SHA256,SHA384,SHA512,MD5 algorithm.

1. Use the Hashlib module for MD5 encryption.

Import= hashlib.md5 () m.update (b"Hello") m.update (b " It ' s me " )print(m.hexdigest ()) m.update (b"It ' s been a long time since last Time we ... " )print(M.digest ())

Note: Hashlib.md5 (): Creates a MD5 encryption parameter.

Note: the variable. hexdigest (): Hexadecimal encryption.

Note: Variables. Digest (): Binary encryption

Note: There are a total of 32 values.

2. Encrypt using the SHA1 algorithm.

Import= hashlib.sha1 () s2.update (b"ABC")Print (S2.hexdigest ())

Note: There are a total of 38 values.

3. Encrypting using the SHA256 algorithm

S2 = hashlib.sha256 () s2.update (b"ABC")print( S2.hexdigest ())print(len (S2.hexdigest ()))

Note: There are 64 encrypted values.

4. Encrypt using the SHA384 algorithm.

Import= hashlib.sha384 () s2.update (b"ABC")Print  (S2.hexdigest ())print(len (S2.hexdigest ()))

Note: There are 96 encrypted values.

5. Encrypt using the SHA512 algorithm.

Import= hashlib.sha512 () s2.update (b"ABC")Print  (S2.hexdigest ())print(len (S2.hexdigest ()))

Note: There are 128 encrypted values.

Two. Advanced Encryption algorithm

Import= hmac.new ('wueiqi') h.update ('Hellowo ' )print h.hexdigest ()

Note: the equivalent of encryption and then a layer of encryption. Double-layer encryption.

Import= hmac.new (b"xsk","y wind x". Encode ( encoding="utf-8")print(h.digest ()) print (H.hexdigest ())

Note: double-layer encryption.

Note: To add a Chinese key, convert the character type.

Python hashlib Module

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.