Abstract Python algorithm MD5 SHA1 introduction and application example code

  • 2020-06-23 01:06:28
  • OfStack

As for learning algorithms, this site feels that most algorithms in programming languages have something in common. The main aspect 1 is to understand what the algorithm can be used for, and the other aspect is to learn how it can be implemented in such programming languages.

Algorithm is also called hash algorithm and hash algorithm. It converts arbitrary length of data into a fixed length string (usually represented as a hexadecimal string) using a function. Abstract algorithm is to calculate fixed-length abstract digest for arbitrary length data by using the abstract function f(), in order to find out whether the original data has been tampered with (different data calculate different abstracts).

Common summary algorithms are MD5 and SHA1

MD5


import hashlib
m=hashlib.md5()
m.update('zhangkang')
print(m.hexdigest())

 The output :
09b32682a49db34d3c9d7e6d97f85a4a

If the data is too long, you can call update() multiple times and get one result


import hashlib
m=hashlib.md5()
m.update('zhang')
m.update('kang') # The output 1 sample 
print(m.hexdigest())

 Output: 
09b32682a49db34d3c9d7e6d97f85a4a

Suppose we change one letter in the original data to see if the calculated value of MD5 is completely different


import hashlib
m=hashlib.md5()
m.update('zhangkanf')# Not at all 1 Like, although only changed 1 A letter 
print(m.hexdigest())

 Output: 
17d2bcf39906311768c2f363778d2801

MD5 is the most common summary algorithm, which is fast and produces a fixed 128 bit bytes, 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, update () is also supported, with some differences in the output. The result of SHA1 is 160 bit bytes, usually represented by a 40-bit hexadecimal string.

Abstract Algorithm Application

If we have a website with user names and passwords stored in the database, and assume that the passwords in the database are in clear text, then once the database is leaked, the passwords of all users will be obvious. This may lead to user information disclosure, and the correct way to save the user password is not to save the plaintext password, but to save the password MD5 value. When the user logs in, the MD5 value of the password is calculated first, and then compared to the database. One might ask, what if the MD5 value of the password is compromised? This doesn't matter because it is convenient to calculate the MD5 value for the data, but it is nearly impossible to extrapolate the original data from the MD5 value. To protect the user's password information more securely, it is recommended to calculate the MD5 value of the password together with the user name, password, or other fixed string of 1 and update(), also known as "add salt".


# Simulated user login 
import hashlib
db={
'zhangkang':'25c25c67943e82a116ec8c32218a5068',
}
# The 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!')

conclusion

Above is the summary algorithm MD5, SHA1 in Python introduction and application code of the whole content, hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: