Python hashlib Module

Source: Internet
Author: User
Tags hmac sha1

Hashlib Module

    • For cryptographic related operations, instead of the MD5 module and the SHA module
    • Mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm
# MD5 Obsolete Import Md5hash = Md5.new () hash.update (' admin ') print hash.hexdigest ()

# sha Obsolete Import Shahash = Sha.new () hash.update (' admin ') print hash.hexdigest ()

 hashlib encryption:

import hashlib# ######## md5 ####### #hash = HASHLIB.MD5 () hash.update ("Evescn" . Encode (' Utf-8 ')) print (Hash.hexdigest ()) # ######## SHA1 ####### #hash = HASHLIB.SHA1 () hash.update ("Evescn". Encode (' Utf-8 ') print (Hash.hexdigest ()) # ######## sha256 ####### #hash = hashlib.sha256 () hash.update ("Evescn". Encode (' Utf-8 ') ) Print (Hash.hexdigest ()) # ######## sha384 ####### #hash = hashlib.sha384 () hash.update ("Evescn". Encode (' Utf-8 ')) Print (Hash.hexdigest ()) # ######## sha512 ####### #hash = hashlib.sha512 () hash.update ("Evescn". Encode (' Utf-8 ')) print ( Hash.hexdigest ()) # Input Result: 48fc6919f119605c4064ea7d81ac6b34f2e9afa0fbde5b05a444f7bc75d6695fbb84b8d005406e832ad8456184cd36fd3bd88dd1fff2392eb873 a1fe7aff9e6332bd7a353e8027e7a2aa54ba604fbe66f853212f23b4fbae80883972fc8a24a92c04b9cb7c3ee01840ccb98f5c3edf897d15f9fae7931 c755048f72babc0b6f016c1581a51c4af7d3bef64a1c21156866a68cccf37d6914bacde9f0d7b83e328bc2025847f6754b6eec57be91f06e6ad5ca17d 1b 

python3,2 type of encryption writing format

Import hashlib# ######## md5 ####### #hash = HASHLIB.MD5 () hash.update ("Evescn". Encode (' Utf-8 ')) print (Hash.hexdigest () ) hash = HASHLIB.MD5 () hash.update (b "EVESCN") print (Hash.hexdigest ()) # Input Result: 48fc6919f119605c4064ea7d81ac6b3448fc6919f119605c4064ea7d81ac6b34

Although the above encryption algorithm is still very strong, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.

Import hashlib# ######## md5 ####### #hash = HASHLIB.MD5 ("Evescn". Encode (' Utf-8 ')) hash.update ("Evescn". Encode (' Utf-8 ') ) Print (Hash.hexdigest ()) # input Result: d6fd21d81a89fa360fe2ee37c6417b6f

Python also has an HMAC module that internally creates keys and content for us to process and then encrypt

Import Hmach = Hmac.new (b "EVESCN") h.update (b "EVESCN") print (H.hexdigest ()) # input Result: bd4e50d0a9c686fad0a0be089ba918be

Python hashlib Module

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.