Python Learning Notes-basic "Sixth Week"--hashlib module

Source: Internet
Author: User
Tags hmac sha1

Hashlib module of common modules

For cryptographic related operations, the 3.x replaces the MD5 module and the SHA module, mainly providing SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm

Import= md5.new () hash.update ('admin')Print Hash.hexdigest ()
md5-Waste
Import= sha.new () hash.update ('admin')print Hash.hexdigest ()
sha Waste
Import Hashlib # ######## MD5 ######## hash = HASHLIB.MD5 () hash.update (' admin ') print hash.hexdigest () # ######## SHA1 # # # # # # # hash = HASHLIB.SHA1 () hash.update (' admin ') print hash.hexdigest () # ######## sha256 ######## hash = hashlib.sha256 () ha Sh.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 ()

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 (' 898oafs09f ') hash.update (' admin ') print hash.hexdigest ()

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

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

No more!!!

1 ImportHashlib2  3m =hashlib.md5 ()4M.update (b"Hello")5M.update (b"It ' s me")6 Print(M.digest ())7M.update (b"It ' s been a long time since last time we ...")8  9 Print(M.digest ())#2 binary format hashTen Print(Len (M.hexdigest ()))#16 binary format hash One " " A def digest (self, *args, **kwargs): # Real Signature Unknown - "" " Return the Digest value as a string of binary data. " " - Pass the   - def hexdigest (self, *args, **kwargs): # Real Signature Unknown - "" " Return the Digest value as a string of hexadecimal digits. " "" " - Pass +   - " " + ImportHashlib A   at ######### MD5 ######## -   -hash =hashlib.md5 () -Hash.update ('Admin') - Print(Hash.hexdigest ()) -   in ######### SHA1 ######## -   tohash =hashlib.sha1 () +Hash.update ('Admin') - Print(Hash.hexdigest ()) the   * ######### sha256 ######## $  Panax Notoginsenghash =hashlib.sha256 () -Hash.update ('Admin') the Print(Hash.hexdigest ()) +   A   the ######### sha384 ######## +   -hash =hashlib.sha384 () $Hash.update ('Admin') $ Print(Hash.hexdigest ()) -   - ######### sha512 ######## the   -hash =hashlib.sha512 ()WuyiHash.update ('Admin') the Print(Hash.hexdigest ())
View Code

Read more about md5,sha1,sha256 and other introductory articles here https://www.tbs-certificates.co.uk/FAQ/en/sha256.html

Python Learning Notes-basic "Sixth Week"--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.