Digest algorithm MD5, SHA1 introduction and application instance code in Python, pythonsha1
For algorithm learning, I think most of the algorithms in programming languages are in the same place. The main aspect is to understand what this algorithm can be used for. On the other hand, learn how it is implemented in such programming languages.
Digest algorithms are also called hash algorithms and hash algorithms. It converts data of any length into a fixed-length data string (usually expressed in hexadecimal strings) through a function ). The digest algorithm is to use the digest function f () to calculate a fixed-length digest for data of any length, the purpose is to find out whether the original data has been tampered with (different summaries calculated by different data ).
Common digest algorithms include MD5 and SHA1.
MD5
import hashlib
m = hashlib.md5 ()
m.update ('zhangkang')
print (m.hexdigest ())
Output:
09b32682a49db34d3c9d7e6d97f85a4a
If the data is too long, you can call update () multiple times, the result is the same
import hashlib
m = hashlib.md5 ()
m.update ('zhang')
m.update ('kang') #The output result is the same
print (m.hexdigest ())
Output:
09b32682a49db34d3c9d7e6d97f85a4a
If we change a letter in the original data to see if the calculated MD5 value is completely different
import hashlib
m = hashlib.md5 ()
m.update ('zhangkanf') # The output is completely different, although only one letter is changed
print (m.hexdigest ())
Output:
17d2bcf39906311768c2f363778d2801
MD5 is the most common digest algorithm. It is very fast, and the generated result is a fixed 128 bit byte, usually represented by a 32-bit hexadecimal string.
SHA1
import hashlib
s = hashlib.sha1 ()
s.update ('my name is zhangkang')
print (s.hexdigest ())
Output:
512e877d47cd06246b24ac99027991cbfa67aec1
Similar to MD5, it also supports multiple update () in blocks, but the output results are slightly different. The result of SHA1 is 160 bit bytes, usually represented by a 40-bit hexadecimal string.
Summary algorithm application
If we have a website that stores information such as user names and passwords in the database, assuming that the user passwords in the database are all plain text, then once the database is leaked, the passwords of all users will be obvious. This may lead to leakage of user information, and the correct way to save the user password is not to save the clear text password, but to save the MD5 value of the password. When the user logs in, the MD5 value of the password is calculated first, and then compared with the database. Someone may ask, what if the MD5 value of the password is leaked? This does not matter, because it is very convenient to calculate the MD5 value of the data, but it is basically impossible to reverse the original data from the MD5 value. In order to protect the user's password information more securely, when calculating the MD5 value of the password, it is recommended to update () together with the user name, password, or other fixed string, also known as "salt".
#Simulate user login
import hashlib
db = {
'zhangkang': '25c25c67943e82a116ec8c32218a5068',
}
#Plaintext password is: zhangkang123456
def login (username, password):
m = hashlib.md5 ()
m.update (username + password + 'the-salt')
passwd = m.hexdigest ()
if passwd! = db [username]:
return False
else: return True
while (True):
username = raw_input ('Input username:')
password = raw_input ('Input password:')
if (login (username, password)):
print ('login success!')
break
else:
print ('login failed!')
to sum up
The above is all about the introduction of the digest algorithm MD5 and SHA1 in Python and the application example code in this article, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are deficiencies, please leave a message to indicate. Thank you friends for your support!