hashlib
1.MD5
MD5是最常见的摘要算法,速度很快,生成结果是固定的128 bit字节,通常用一个32位的16进制字符串表示。
def get_md5(word):
md5 = hashlib.md5()
md5.update(word.encode('utf-8'))
print(md5.hexdigest())
2.SHA1
SHA1的结果是160 bit字节,通常用一个40位的16进制字符串表示。
#生成sha1
def hashlib_sha1():
sha1 = hashlib.sha1()
sha1.update('hanqingqing'.encode('utf-8'))
print(sha1.hexdigest())
常用于数据库存储密码以非明文方式存储,验证登录时账号密码是否匹配等。
def save_psw_yaml():
message = {}
input_word = input('input:').split(':')
message[input_word[0]] = hashlib_md5(input_word[1])
file = open('dump.yaml','a',encoding='utf-8')
yaml.dump(message,file)
file.close()
def login(user,password):
password_md5 = hashlib_md5(password)
file = open('dump.yaml','r',encoding='utf-8')
yaml_data = yaml.safe_load(file.read())
if yaml_data.get(user) == password_md5:
print('True')
else:
print('False')
#save_psw_yaml()
user = input('input-user:')
password = input('input-password:')
login(user,password)