# This is a learning note for the Liaoche teacher Python tutorial
1 , overview
Python's hashlib provides a common digest algorithm, such as md5,sha1 and so on.
Abstract the algorithm is also called hash algorithm and hashing algorithm. It uses a function to convert any length of data into a fixed length data string (usually represented by a 16-binary string).
1) MD5 the application
Import Hashlib
md5 = hashlib.md5 () # hashlib module md5 () function, created a md5 class
md5. update (' How to use MD5 in Python hashlib? '). Encode (' Utf-8 ') # call update () method encoding
print (md5.hexdigest ()) # Print + bit - the binary string
If the amount of data is large, you can call the update () method multiple times in chunks
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.
2) SHA1 the application
Import Hashlib
SHA1 = HASHLIB.SHA1 ()
Sha1.update (' How to use SHA1 in '. Encode (' Utf-8 '))
Sha1.update (' python hashlib? '). Encode (' Utf-8 '))
Print (Sha1.hexdigest ())
The result of the SHA1 is a bit byte, which is usually represented by a 40-bit 16 binary string.
3) Add salt
Add a complex string to the original password to achieve encryption, commonly known as "add salt"
def calc_md5 (password):
return Get_md5 (password + ' The-salt ')
2 , examples
1 , design a function that verifies the user's login, and returns TRUE or false depending on whether the password entered by the user is correct:
#-*-Coding:utf-8-*-
db = {
' Michael ': ' e10adc3949ba59abbe56e057f20f883e ',
' Bob ': ' 878ef96e86145580c38c87f0410ad153 ',
' Alice ': ' 99B1C2188DB85AFEE403B1536010C2C9 '
}
Import Hashlib
def login (user, password):
MD5=HASHLIB.MD5 ()
Md5.update (Password.encode (' Utf-8 '))
If Md5.hexdigest () ==db[user]:
Return True
Else
Return False
Test
Assert login (' Michael ', ' 123456 ')
Assert login (' Bob ', ' abc999 ')
Assert login (' Alice ', ' alice2008 ')
Assert not Login (' Michael ', ' 1234567 ')
Assert not Login (' Bob ', ' 123456 ')
Assert not login (' Alice ', ' Alice2008 ')
Print (' OK ')
2 , simulate user registration based on login name and password entered by user, calculate more secure MD5
db = {}
def register (username, password):
Db[username] = get_md5 (password + username + ' The-salt ') # set key 's value
#-*-Coding:utf-8-*-
Import Hashlib, Random
def get_md5 (s):
return Hashlib.md5 (S.encode (' Utf-8 ')). Hexdigest () # generate MD5 value
Class User (object):
def __init__ (self, username, password):
Self.username = Username
# generate salt. randint () generates a random integer in the range. chr (), which generates an ASCII character corresponding to an integer . for loop three times
Self.salt = ". Join ([Chr (Random.randint (122)) for I in Range])
Self.password = get_md5 (password + self.salt)
db = {
' Michael ': User (' Michael ', ' 123456 '),
' Bob ': User (' Bob ', ' abc999 '),
' Alice ': User (' Alice ', ' alice2008 ')
}
DEF login (username, password):
user = Db[username]
return User.password = = GET_MD5 (password+user.salt)
Test
Assert login (' Michael ', ' 123456 ')
Assert login (' Bob ', ' abc999 ')
Assert login (' Alice ', ' alice2008 ')
Assert not Login (' Michael ', ' 1234567 ')
Assert not Login (' Bob ', ' 123456 ')
Assert not login (' Alice ', ' Alice2008 ')
Print (' OK ')
Python Learning note __12.5 hashlib