3.7 hashlib模块
-
加密模块, 摘要算法,散列算法,等等.它是一堆加密算法的集合.
-
hashlib如何加密?
-
将一个bytes类型的数据 通过hashlib进行加密返回 一个等长度的16进制数字.
-
过程不可逆.
-
相同的bytes类型的数据通过相同的加密方法得到的数字绝对相同.
-
不相同的bytes类型的数据通过相同的加密方法得到的数字绝对不相同.
-
-
撞库: 111111, 123456, 000000,19980123,
{'202cb962ac59075b964b07152d234b70': 123456}
-
hashlib 的用途:
-
密码加密.
-
文件一致性校验.
-
1.密码加密:
import hashlib
ret = hashlib.md5()
ret.update('123'.encode('utf-8'))
s = ret.hexdigest()
print(s,type(s)) 202cb962ac59075b964b07152d234b70 <class 'str'>
import hashlib
ret = hashlib.md5()
ret.update('123'.encode('utf-8'))
s = ret.hexdigest()
print(s,type(s)) 202cb962ac59075b964b07152d234b70 <class 'str'>
两个一样
import hashlib
ret = hashlib.md5()
ret.update('123fkejfkefjdefekfefka'.encode('utf-8'))
s = ret.hexdigest()
print(s,type(s)) 3ebcde7d2fc16401c8b42a7994ca34d4 <class 'str'>长度一样
2.加固定的盐
ret = hashlib.md5('xxx教育'.encode('utf-8'))
ret.update('123456'.encode('utf-8'))
s = ret.hexdigest()
print(s,type(s))
3.加动态的盐
username = input('输入用户名:').strip()
password = input('输入密码').strip()
ret = hashlib.md5(username[::2].encode('utf-8'))
ret.update(password.encode('utf-8'))
s = ret.hexdigest()
print(s)
4.sha 系列
sha系列: 安全系数高,耗时高.
加盐,加动态盐
ret = hashlib.sha512()
ret.update('123456fdklsajflsdfjsdlkafjafkl'.encode('utf-8'))
s = ret.hexdigest()
print(s,type(s))
5.文件的一致性校验
low版
import hashlib
ret = hashlib.md5()
with open('MD5文件校验',mode='rb') as f1:
content = f1.read()
ret.update(content)
print(ret.hexdigest())
ret = hashlib.md5()
with open('MD5文件校验1',mode='rb') as f1:
content = f1.read()
ret.update(content)
print(ret.hexdigest())
ret = hashlib.md5()
with open(r'D:\s23\day17\python-3.7.4rc1-embed-win32.zip',mode='rb') as f1:
content = f1.read()
ret.update(content)
print(ret.hexdigest())
d9c18c989c474c7629121c9d59cc429e
d9c18c989c474c7629121c9d59cc429e
分布update
分步update
s1 = '大学 最好的python'
1
ret = hashlib.md5()
ret.update(s1.encode('utf-8'))
print(ret.hexdigest())
2
ret = hashlib.md5()
ret.update('大学'.encode('utf-8'))
ret.update(' 最好的python'.encode('utf-8'))
print(ret.hexdigest()) # 90c56d265a363292ec70c7074798c913
hegit版
import hashlib
def md5_file(path):
ret = hashlib.md5()
with open(path,mode='rb') as f1:
while 1:
content = f1.read(1024)
if content:
ret.update(content)
else:
return ret.hexdigest()
print(md5_file(r'D:\s23\day17\python-3.7.4rc1-embed-win32.zip'))
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· Ai满嘴顺口溜,想考研?浪费我几个小时