Python Learning Note-Hashlib module

Source: Internet
Author: User

Python can be used to encrypt files via Hashlib. Hashlib can support a variety of encryption, the most common MD5 for example, we can use to encrypt the user's input password, and then saved in the database.


Example 1 encrypted string

Import hashlibobj = hashlib.md5 (bytes (' 1111 ', encoding= ' Utf-8 ')) obj=hashlib.md5 () obj.update (bytes (' 123 ', encoding= ' UTF8 ') obj.update (bytes (' 456 ', encoding= ' utf8 ') result = Obj.hexdigest () print (obj) print (obj.digest ()) Print (Result )----------------<md5 HASH Object @ 0x101d839b0>b ' \xe1\n\xdc9i\xbay\xab\xbev\xe0w\xf2\x0f\x88> ' e10adc3949ba59abbe56e057f20f883e


Notable points are:

    1. First, an object is generated by HASHLIB.MD5 (), and then the method of this object is called to update to generate the corresponding MD5 checksum, and then print it out;

    2. I performed the update operation on the string ' 123 ', ' 456 ' bytes, then finally his MD5 result and the ' 123456 ' byte are actually the same. In a similar way, we can read a very large file in bytes.

    3. The difference between hexdigest () and digest () is that the former output is 2 binary and the latter is a 16-binary summary

    4. Because the MD5 can be cracked through the collision, so can be obj=hashlib.md5 (bytes (' 111 ', encoding= ' utf8), and then add a secret, so the probability of the pool is greatly reduced.


Example 2 encrypting a file

Import hashlibdef MD5 (fname): Hash_md5 = HASHLIB.MD5 () with open (fname, "RB") as F:for Chunk in ITER (lambda: F.read (4096), B ""): Hash_md5.update (chunk) return hash_md5.hexdigest () dd=md5 (' index.py ') print (DD)---------- -------------06fd2a2b98ff2e333bc9861dfb8048de

Precautions:

Because the file can be large, only 4,096 bytes are read at a time for conversion. Finally return to the final MD5 checksum.

Note that the use of ITER (), the first parameter is the return of the F.read (4096) bytecode iterator, the second parameter B "" means that until this parameter is the end of the iteration, in other words, the equivalent of a loop, each read 4,096 byte code, until the empty

For chunk in ITER (Lambda:f.read (4096), B "")


The final MD5 result is the same as I do in OSX shell.

JIP-ISE:WEEK6 yli$ MD5 index.py MD5 (index.py) = 06fd2a2b98ff2e333bc9861dfb8048de



This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1854933

Python Learning Note-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.