Hashlib Module
Hashlib is a module that provides character encryption, including MD5 and SHA encryption algorithms, specifically supporting MD5,SHA1, sha224, sha256, sha384, sha512 and other algorithms. This module is widely used in user login authentication, and it is also very common for text encryption. Module calls are simple, so let's take a look.
Basic call:
1 Import Hashlib 2 new_md5 = HASHLIB.MD5 () # Create MD5 object for Hashlib 3 new_md5.update (' String ') # loads the string into the MD5 object and obtains the MD5 algorithm encryption. 4print(New_md5.hexdigest ()) # through the Hexdigest () method, get the New_ The 16 binary MD5 of the MD5 object is displayed.
In simple terms. is three steps:
1. Set up a cryptographic object. 2, the string is encrypted with the algorithm. 3, get 16 binary display
The cryptographic algorithms that can be created are: MD5,SHA1, sha224, sha256, sha384, sha512
1 Example: 2 NEW_MD5 = hashlib.md5 () 3 new_sha1 = hashlib.sha1 () 4 new_ sha256 = hashlib.sha256 ()
Simple invocation:
1 new_md5 = hashlib.new ('MD5', b' string ' ). Hexdigest ()
Note: When passing strings to an object, the encoding type must be. You can use the method of the string before B ' or use the. Encode (' UTF-8 ') method to make the string into the bytes type.
Below we will introduce a simple strength to illustrate the most commonly used MD5 algorithm in practical applications. Before the introduction of strength, let us first understand the basic characteristics of MD5.
Features of the MD5 algorithm:
- Compressibility: Any length of data, the length of the calculated MD5 value is fixed.
- Easy to calculate: The MD5 value calculated from the original data is easy. No matter how big the data is, a string of MD5 strings can be calculated quickly.
- Anti-modification: Make any changes to the original data, even if you modify any one of the bytes, the generated MD5 value will be a big difference.
- Strong anti-collision: Known raw data and MD5, it is very difficult to find a data with the same MD5 value (that is, falsification of data).
Well, we already know the features of the MD5 algorithm. So here's the problem, MD5 since each generated value is fixed. So although a one-way generation can not be reversed, but if the collision method on the string generation MD5 after the collision with the MD5 value, is not the MD5 corresponding string? That's correct! This is possible, in other words, if a user database is compromised, the MD5 in the vault will be able to hack the password through a collision method. Then the question comes again, and since then, that's no use. No, let's use a little technique to make this collision method ineffective. This method is "add salt", what is salt?
"Adding salt" is to add an extra string to the original password, and then generate the MD5 value, so there is no way to crack, unless you get the "salt" string, but the collision method is also required to recalculate the MD5 value after the collision comparison, the difficulty is extremely large. So look at the following code:
Example: MD5 password Authentication, with "Add salt" method
1 ImportHashlib2 3Yan ='! Any # meaning% character ^ symbol @' #defining salt-added strings4PWD = input ('>>>')5 6Md5_pwd =hashlib.md5 ()7Md5_pwd.update ((Pwd+yan). Encode ('UTF-8'))#Add Salt8PWD =md5_pwd.hexdigest ()9 #pwd = hashlib.new (' MD5 ', (Pwd+yan). Encode (' UTF-8 ')). Hexdigest () #也可以这样简写哦: A word is done.
Chestnut is shorthand, but nutrition in, delicious not difficult.
Python hashlib Module