day22- hashlib modules - digest algorithm (hash algorithm)

# Python's hashlib provides a summary of common algorithms, such as md5 (md5 algorithm), sha1 and so on. Abstract: Digest 
# digest algorithm, also known as hash algorithm, hash algorithm. 
# It through a function, data (plaintext) is converted to a length of any fixed-length data string (ciphertext) (typically a string representation of a hexadecimal). 
# Digest algorithm is the digest by function f () of an arbitrary length data DATA is calculated digest fixed-length 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. 
# Pay attention to digest although the algorithm can be used for encryption, but there are many online sites can be decrypted, there is a certain risk of being cracked. 
# A ciphertext corresponding to the plaintext unique. Commonly used in contrast login password and password stored in a computer or Web site password (check the consistency of the file). 
# If you save the password in clear text (user name or password), if the database is compromised, all the user's password fall into the hands of hackers. Hackers hit library. 
# In addition, site operation and maintenance personnel can access the database, which is able to obtain all the user's password. 
# Correct way is to save the password does not store user passwords in plain text, but rather a summary store user passwords, such as MD5 


#1. 
Import hashlib 
MD5 = hashlib.md5 () # MD5 can be changed sha1, sha1 is more secure than the algorithm sha256 and sha512, but the slower the more secure algorithms, and digest length longer. 
md5.update (B ' 136.32 thousand ' )
 Print (md5.hexdigest ()) # d5d082d2642302fae506350fff337632, which is md5 value, a 32-bit hexadecimal character string. 

# 2. If a large amount of data, the block can be called multiple times Update (), the final calculation result is the same: 
Import hashlib 
MD5 = hashlib.md5 () 
md5.update (B ' 13 is ' ) 
md5.update (B ' 6320 ' ) # 13 with 6320 values superimposed with md5 md5 value 136 320 is the same. 
Print (md5.hexdigest ()) #d5d082d2642302fae506350fff337632 
# md5.update (b'136320 ') 
# Print (md5.hexdigest ()) d5d082d2642302fae506350fff337632 # 

# 3. registered user, the password value is md5: 
Import hashlib 
username = INPUT ( ' username: ' ) 
password = INPUT ( ' password : ' ) 
md5 = hashlib.md5 () # Get md5 objects 
md5.update (bytes (password, encoding = ' UTF-. 8 ' )) # encryption: md5.update password 
md5_pwd md5.hexdigest = () # Get value md5 ( ciphertext) 
with Open ( ' username password &',mode = 'w',encoding = 'utf-8') as f:
    f.write('%s&%s'%(username,md5_pwd))


# 4. 用户登录验证:
import hashlib
username = input('username:')
password = input('password:')
with open('username&password',encoding = 'utf-8') as f:
    for line in f:
        user,pwdLine.split = ( ' & ' ) 
    MD5 = hashlib.md5 () 
    md5.update (bytes (password, encoding = ' UTF-. 8 ' ))
     # write the encoding = 'utf-8', or will be thrown string AN encoding the without argument 
    md5_pwd = md5.hexdigest ()
     IF username == the User and md5_pwd == pwd:
         Print ( ' Login successful ' )
     the else :
         Print ( ' account or password wrong ' ) 

#5. Salt: After MD5 password Salt treatment so far as Salt is not a hacker knows, even if the user enters the password is simple, it is difficult to push through anti-MD5 passwords in plain text, but there are certain risks to break. 
Import hashlib 
MD5 = hashlib.md5 (bytes ( ' Salt ' , encoding = ' UTF-. 8 ' )) # Salt can be any character. 
md5.update (B ' 136.32 thousand ' )
 Print (md5.hexdigest ()) # dc740385c07799eefb7c1f1ae24050ee 
# added salt after, md5 d5d082d2642302fae506350fff337632 value is not the same as with the original. 

# Later salts may also add other characters: b'123 ' 
Import hashlib 
MD5 = hashlib.md5 (bytes ( ' Salt ' , encoding = 'utf-8') + b'123')
md5.update(b'136320')
print(md5.hexdigest())

 

Guess you like

Origin www.cnblogs.com/python-daxiong/p/11304126.html