Digest algorithm MD5, SHA1 introduction and application instance code in Python, pythonsha1

Source: Internet
Author: User

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.


import hashlib
m = hashlib.md5 ()
m.update ('zhangkang')
print (m.hexdigest ())
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 ())

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 ())

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.


import hashlib
s = hashlib.sha1 ()
s.update ('my name is zhangkang')
print (s.hexdigest ())

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!')
    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!

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.