Python Common Module--hashlib module

Source: Internet
Author: User
Tags sha1

Python's hashlib provides a common digest algorithm, such as MD5, SHA1, etc.

What is a digest algorithm? Abstract the algorithm is also called hash algorithm and hashing algorithm.

It transforms any length of data into a fixed-length data string (usually expressed in hexadecimal strings) through a function.

Abstract algorithm is the summary function f () for any length of data to calculate the fixed length of the Digest digest, you can check whether the data you want to be tampered with.

The abstract algorithm can indicate whether the data has been tampered with, because the digest function is a one-way function, and it is easy to calculate f (data), but it is very difficult to digest data by using the.

Also, making a bit change to the original data will result in a completely different summary of the calculations.

We take the most common MD5 as an example:

Import= hashlib.md5 ()   # Select the MD5 class in the digest algorithm to instantiate, get MD5md5.update (b"  Everything is passible")   # a digest of a string print(Md5.hexdigest ())   # Find a summary algorithm to get results

The calculation results are as follows:

A8c816f56c74b68e4d547769531a46d1

If the data is large, you can call Update () multiple times for a summary, and the result is the same:

Import= hashlib.md5 () md5.update (b"everything") md5.update ( b"is  passible")print(Md5.hexdigest ())

MD5 is the most common digest algorithm and is fast enough to generate a fixed 128bit byte, typically represented by a 32-bit 16-character string.

Another common digest algorithm is SHA1, which calls SHA1 and calls MD5 exactly like:

Import= hashlib.sha1 () sha1.update (b"everything is passible"  )Print(Sha1.hexdigest ())

Calculation Result:

000da64f15dce120bd685b189c3e169a1f67753b

The result of the SHA1 is 160bit bytes, typically represented by a 40-bit hexadecimal string.

Algorithms that are more secure than SHA1 are sha256 and sha512, but the more secure the algorithm is, the slower it is, and the longer the digest length.

Hashlib generally in the file consistency check and encryption authentication both use more.

Because the MD5 value of the common password is easy to calculate, we usually add some operations, such as "Add Salt", which is done by adding a complex string to the original password. Like what:

# some special strings are added before and after, which reduces the chance of occurrence.  Import= hashlib.md5 () md5.update (b"*) _#$%^&everything is passible %^&")print(Md5.hexdigest ())

Summary:

Abstract algorithms are widely used in many places. Note that the digest algorithm is not an encryption algorithm and cannot be used for encryption (because plaintext cannot be reversed by a digest) and can only be used for tamper protection and client authentication.

Python Common Module--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.