hashlib

时间:2019-11-16
本文章向大家介绍hashlib,主要包括hashlib使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

hashlib

hashlib是一个加密模块,提供了常见的摘要算法,如MD5,SHA1

MD5算法加密检验数据完整性

所谓摘要算法,也可以称为:哈希算法,离散算法。即通过一个函数,将任意长度的数据转化为一个长度固定的数据串(通常16进制)

摘要算法:

​ 摘要一样,内容就一定一样:保证唯一性


密文密码就是一个摘要

import hashlib

def pwd_md5(pwd):
    md5_obj = hashlib.md5()   #创建一个md5对象
    str1 = '1234'   
    md5_obj.update(str1.encode('utf-8'))    #update中一定要传入bytes类型数据
    sal = '加盐加盐'
    md5_obj.update(sal.encode('utf-8'))     #为了防止撞库,加盐

    res = md5_obj.hexdigest()       #变成16进制
    print(res)
    
#验证
with open('user.txt','r',encoding='utf-8') as f:        #打开文件
    user_str = f.read()                             #读取文件
    
file_user,file_pwd = user_str.strip(':')        #用户名,密码  切分

username = input("输入用户名:").strip
password = input("输入密码:").strip

if username == file_user and pwd_md5(password) == file_pwd: #将输入的md5加密,与文件里的相比
    print("登陆成功")
else:
    print("登陆失败")
    

file_user,file_pwd = user_str.strip(':')

原文地址:https://www.cnblogs.com/leiting7/p/11873860.html