Module: digest algorithm, hashlib

Algorithm description:

Python's hashlib provides a common digest algorithm, such as MD5, SHA1, and so on.

What is the digest algorithm it? Digest algorithm, also known as hash algorithm, hash algorithm. It is through a function to convert data for any length of a fixed length data string (typically represented by a string of 16 hexadecimal).

Digest by the digest algorithm is the function f () of an arbitrary length data DATA fixed length calculated summary digest, in order to discover whether the original data had been tampered with.

Digest algorithm is able to indicate whether the data has been tampered with, because digest function is a one-way function, calculate f (data) is easy, but it is very difficult to digest data by reverse thrust. Also, do a bit of modifications to the original data, will lead to the calculated digest completely different.

hashlib: Digest Algorithm

    General Summary

    Salt summary

    Dynamic salt summary

# General summary 
Import hashlib 
md = hashlib.md5 ()    # obtain a digest in md object 
md.update (B ' 1234 ' )    # Update method, requiring an abstract incoming string of bytes must type 
# md.update ( bytes (password, encoding = 'utf -8')) #password dynamic string, the same type need to be converted to bytes, specifies the character encoding 
RET = md.hexdigest # hexdigest hashlib of a method, there is a digest value points memory address 
RET = md.hexdigest () # plus () Gets the digest value 

# if the data is large, the block can be called multiple times Update (), the result is the same as the last calculated: 
MD5 = hashlib.md5 () 
MD = hashlib.md5 () 
md5.update ( ' How to use MD5 in '  )
md5.update ('python hashlib?')
md.update('how to use md5 in python hashlib?')
print(md5.hexdigest())
print(md.hexdigest())

#打印:
d26a53750bc40b38b65a520292f69306
d26a53750bc40b38b65a520292f69306

 

 

Note: 
MD5 is a class hashlib module belonging to an algorithm. There sha1, sha224 algorithms. 
update belongs to the method algorithm for incoming requiring an abstract parameters.
hexdigest is hashlib a method for acquiring the digest value.
If a large amount of data, the block can be called multiple times Update (), the result is the same as the last calculated

Salt Abstract:
hashlib.md5 (salt)
hashlib.md5 = MD (B ' Aike ' ) # B, the default is ASCII code, do not support the Chinese 
MD1 = hashlib.md5 (bytes ( ' Ike ' , encoding = ' UTF-. 8 ' )) # Chinese need bytes manner developing coding 
md.update (B ' 1234 ' ) 
md1.update (B ' 1234 ' ) 
RET = md.hexdigest () 
RET1 = md1.hexdigest ()
 Print (RET)
 Print (RET1)

Dynamic salt:
the INPUT = usr ( ' Please enter your user name ' ) 
md = hashlib.md5 (b ' Aike ' ) # b, the default is ASCII code, do not support the Chinese 
MD1 = hashlib.md5 (bytes ( ' Ike ' , encoding = ' UTF -8 ' ) + bytes (usr, encoding = ' UTF-. 8 ' )) # receiving dynamic parameters of salt 
md.update (B ' 1234 ' ) 
md1.update (B ' 1234 ' ) 
RET = md.hexdigest ( ) 
RET1 = md1.hexdigest ()
 Print (RET)
print(ret1)
 
Application:
user name and password stored in the user's login
# Registration 
DEF info ():
     the try : 
        info_usr = True
         the while info_usr: 
            F = Open ( ' the user_data ' , ' a + ' , encoding = ' UTF-. 8 ' ) # in with form 
            f.seek (0) # open the file a + form , if you want to traverse the file's contents, move the cursor to the beginning position 
            for Line in f: 
                username = the iNPUT ( ' Please enter your user name: ' ) 
                password = the iNPUT ( ' Please enter your password:' ) 
                Usr, pwd = line.split ( ' | ' )
                 IF usr.strip () == username.strip ():
                     Print ( ' account has been registered, please re-enter ' )
                 the else : 
                    MD5 = hashlib.md5 () 
                    md5.update (bytes (password, encoding = ' UTF-. 8 ' )) 
                    info_password = md5.hexdigest () 
                    f.write (username + ' | ' )
                    f.write ( ' % S \ the n- ' % info_password) 
                    info_usr = False 
                    f.close () 
    the except ValueError: # After closing the file thrown prompted to register success 
        Print ( ' successful registration ' ) 

# login 
DEF Longin (): 
    i = . 3
     the while I> 0: 
        I - =. 1 
        with Open ( ' the user_data ' ) AS F: 
            username = iNPUT ( ' enter username: ' )
            password = INPUT ( ' Enter password: ' )
             for Line in F: 
                MD5 = hashlib.md5 () 
                md5.update (bytes (password, encoding = ' UTF-. 8 ' )) 
                info_password = md5.hexdigest () 
                usr, pwd line.strip = () Split (. ' | ' )
             IF usr.strip () == username and pwd.strip () == info_password:
                 Print ( ' Login successful ')
                 BREAK 
            the else :
                 Print ( ' Login failed, please log in again, as well as chances to% s ' % i)
     the else :
         Print ( ' account has been locked ' ) 

# test 
LIS = [
     ' login ' ,
     ' registration ' 
] 

for i in the enumerate (LIS):
     Print (I)
 the while True:
     the try : 
        sELECT = int (iNPUT ( " Please enter or register select number: '))
         IF SELECT == 0: 
            Longin () 
            BREAK 
        elif SELECT. 1 == : 
            info () 
            BREAK 
        the else :
             Print ( " Please enter the correct serial number ' )
     the except Exception:
         Print ( " Please enter the correct serial number ' ) 


knowledge point: hashlib 
            exception handling     
            file reading and writing 
bug: Because as a file access account name and password, can only verify the last line of user name and password

 

Guess you like

Origin www.cnblogs.com/aizhinong/p/11512838.html