23 Common Module hashlib

hashlib, providing digest algorithm module, it is a collection. MD5, SHA1, SHAKE algorithm.
Digest Algorithm: Formal Verification for passwords encrypted data, files. (a, checking the files on a remote server and download files are the same .b, two files on the two machines, check these two files are equal)
digest algorithm principle: for the same string, digest, use the same algorithm value is always the same. Use different algorithms on the same string digest algorithm, the value should be different. Whatever digest algorithm, hashlib of use will never change.

import hashlib
md5 = hashlib.md5()
md5.update(b'123456')
print(md5.hexdigest())	#hexdigest  hex是16进制,digest是摘要

Output:
Here Insert Picture Description
SHA algorithm increases with the complexity of the algorithm, the time cost and space cost summary will be growing.

User login

import hashlib
usr = input('username :')
pwd = input('password : ')
with open('userinfo') as f:
    for line in f:
        user,passwd,role = line.split('|')
        md5 = hashlib.md5()
        md5.update(bytes(pwd,encoding='utf-8'))
        md5_pwd = md5.hexdigest()
        if usr == user and md5_pwd == passwd:
            print('登录成功')

But with the permutations and combinations, all combinations are listed, into a library, so you can hit through the library, to achieve the decryption of salt by the way, we can avoid this situation

With salt

Static salt

import hashlib   # 提供摘要算法的模块
md5 = hashlib.md5(bytes('盐',encoding='utf-8'))		#‘盐’可以是任何的字符
# md5 = hashlib.md5()
md5.update(b'123456')
print(md5.hexdigest())

Dynamic salt

import hashlib
md5 = hashlib.md5(bytes('盐',encoding='utf-8')+b'')  #可以在最后的b''中加入随机的内容,比如用户名的一部分。
# md5 = hashlib.md5()
md5.update(b'123456')
print(md5.hexdigest())

Guess you like

Origin blog.csdn.net/weixin_43265998/article/details/89607418
Recommended