Python Module Learning Hashlib

Source: Internet
Author: User

I. Overview of HASHLIB

Cryptographic Services involved: 14. Cryptographic Services

Where Hashlib is involved in secure hashing and message digests, provides several different cryptographic algorithm excuses, such as SHA1, SHA224, SHA256, SHA384, SHA512, MD5, and so on.

ImportHASHLIBM= HASHLIB.MD5 ()#Create a Hash object, MD5: (message-digest algorithm 5) message digest algorithm, to obtain a 128-bit cipherPrintM#<md5 HASH Object @ 000000000254adf0>M.update ('Beginman')#update Hash object with string parameterPrintM.digest ()#returns the digest as a binary data string valuePrintM.hexdigest ()#returns a hexadecimal numeric string 0b28251e684dfbd9102f8b6f0281c0c5PrintM.digest_size# -PrintM.block_size# -

Use new () to create a hash object that specifies the encryption mode

New (name, string=")     " "" "     Return A new hashing object using the named algorithm;< c6/>optionally initialized with a string.      """
H = hashlib.new ('MD5')PrintH#<md5 HASH Object @ 000000000260bdb0>H2 = Hashlib.new ('ripemd160',' What')PrintH2#<ripemd160 HASH Object @ 000000000271b9f0>H.update ('Beginman')PrintH.hexdigest ()#666fc5baa93a7fb207c5bfff03b67732#equivalents =hashlib.md5 () s.update ('Beginman')PrintS.hexdigest ()#666fc5baa93a7fb207c5bfff03b67732PrintH2.hexdigest ()#9c1185a5c5e9fc54612808977ee8f548b2258d31

Three, Common properties

Print Hashlib.algorithms #(' MD5 ', ' SHA1 ', ' sha224 ', ' sha256 ', ' sha384 ', ' sha512 ') List all cryptographic algorithms
Print H.digest_size #16 produces the hash size of the bytes.
Print H.block_size #Internal block size of the hash algorithm in bytes.

Iv. Common methods

Hash.update (ARG)

Update the hash object with a string argument, m.update (a) if the same hash object repeats the method call; M.update (b) is equivalent to m.update (a+b).

Hash.digest ()

Returns a digest, as a binary data string value,

Hash.hexdigest ()

Returns a summary, as a hexadecimal data string value,

Hash.copy ()

Copy

Recently in the test file copy test, you need to copy the file after the MD5 value comparison, to see if the copy is complete. Google and Baidu are all using the MD5 module to read all the files into memory, in the calculation of MD5, resulting in the calculation of more than 1G size file error. Brother Timespace gives a method for calculating MD5 incrementally, recording:

#!/usr/bin/env pythonImportHashlibImportSYSdefMain ():ifLen (SYS.ARGV)! = 2: Sys.exit ('Usage:%s file'%sys.argv[0]) filename= Sys.argv[1] M=hashlib.md5 () with open (filename,'RB') as FP: whiletrue:blk= Fp.read (4096)#4KB per block            if  notBlk Breakm.update (BLK)Printm.hexdigest (), filenameif __name__=='__main__': Main ()

Python Module Learning Hashlib

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.