Python hashlib module algorithm

Source: Internet
Author: User
Tags md5 digest sha1

Hashlib Module

The hashlib in Python provides us with a common digest algorithm, such as MD5, SHA1

So now the question is, what is the abstract algorithm ?

Abstract the algorithm is also called hash algorithm and hashing algorithm.

It refers to the arbitrary length of data, through the function f (), converted to a fixed length of the Digest digest (usually in 16 binary string representation),

The goal is to find out if the original data has been tampered with.

Abstract the algorithm can tell if the data has been tampered with, just because the digest function is a one-way function,

It is easy to calculate f (data), but it is very difficult to push data back through digest.

And, as long as a little change to the original data, will result in a different summary of the calculation.

Next we take the Common Digest algorithm MD5 as an example,

#计算出一个字符串的MD5值import hashlibmd5 = Hashlib.md5 () md5.update (b ' Hello,python ') #ps: need to be converted to B-byte or encode (' Utf-8 ') print ( Md5.hexdigest ())

Output Result:

15ac32041ff74c93c1842b152df7519e

Import hashlibmd5 = Hashlib.md5 () md5.update (' Hello,python '. Encode (' Utf-8 ')) print (Md5.hexdigest ())

Output Result:

15ac32041ff74c93c1842b152df7519e

If the amount of data is large, you can call Update () multiple times in chunks,

A string is divided into several snippets and the result of a direct summary is the same

Import hashlibmd5 = Hashlib.md5 () md5.update (b ' Hello, ')        #原字符串中有逗号的不要忘记了md5. Update (b ' Python ') print ( Md5.hexdigest ())

Output Result:

15ac32041ff74c93c1842b152df7519e

Copy file check

Import hashlibdef check_md5 (filename):    MD5 = HASHLIB.MD5 ()    with open (filename, ' RB ') as F: While        True:            Content = F.read (2048)    #分段读取 to avoid taking up large segments of memory            if content:                md5.update (content)            else: Break    return Md5.hexdigest ()
File1 = check_md5 (' md5_test1 ') #我创建的md5_test1中的数据为11112
File2 = check_md5 (' md5_test2 ') #我创建的md5_test2中的数据为11111
Print (FILE1)
Print (file2)

Output Result:

afcb7a2f1c158286b48062cd885a9866
b0baee9d279d34fa1dfd71aadb908c3f

MD5 Digest Encrypt incoming passwords and add salt dynamically to improve security

Import Hashlibdef md5_digest (SALT,PWD):    MD5 = HASHLIB.MD5 (Salt.encode (' Utf-8 ')) #salt, add salt operation, increase safety, The salt can also be sliced to further increase safety such as salt[::-1]    md5.update (pwd.encode (' Utf-8 '))    return md5.hexdigest () Salt = ' salt ' pwd = ' 123450 ' Print (Md5_digest (SALT,PWD))

Output Result:

684d41d1f7512e40a8939fd4fed9518a

Salt processing of the MD5 password, as long as the salt is not known by hackers, even if the user entered a simple password, it is difficult to MD5 the plaintext password.

But if two users all use the same simple password such as 123456, in the database, two identical MD5 values will be stored, which means that the password of the two users is the same. Is there a way for users with the same password to store different MD5?

If the user cannot modify the login, it is possible to calculate the MD5 by using the login as part of the salt, so that users who implement the same password also store different MD5.

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 the digest), but it is only used for tamper- proof , but its one-way computing feature determines that the user's password can be verified without storing the plaintext password.

MD5 is the most common digest algorithm and is fast enough to generate a fixed byte of bytes, typically represented by a 32-bit 16 binary string.
Another common digest algorithm is SHA1, call SHA1 and call MD5 exactly the same, is to change the previous MD5 to SHA1 and yourself.
The result of the SHA1 is a bit byte, which is usually represented by a 40-bit 16 binary 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.

Python hashlib module algorithm

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.