Python hashlib模块与subprocess模块使用详细介绍

Welcome ·
更新时间:2024-09-20
· 1179 次阅读

目录

1、什么是哈希hash

2、hash的用途

3、如何用

4、subprocess模块

1、什么是哈希hash

hash一类算法,该算法接受传入的内容,经过运算得到一串hash值

hash值的特点:

只要传入的内容一样,得到的hash值必然一样

不能由hash值返解成内容

不管传入的内容有多大,只要使用的hash算法不变,得到的hash值长度是一定

2、hash的用途

用途1:特点II用于密码密文传输与验证

用途2:特点I、III用于文件完整性校验

3、如何用 import hashlib m=hashlib.md5() m.update('hello'.encode('utf-8')) m.update('world'.encode('utf-8')) res=m.hexdigest() # 'helloworld' print(res) m1=hashlib.md5('he'.encode('utf-8')) m1.update('llo'.encode('utf-8')) m1.update('w'.encode('utf-8')) m1.update('orld'.encode('utf-8')) res=m1.hexdigest()# 'helloworld' print(res)

模拟撞库

cryptograph='aee949757a2e698417463d47acac93df' import hashlib # 制作密码字段 passwds=[ 'alex3714', 'alex1313', 'alex94139413', 'alex123456', '123456alex', 'a123lex', ] dic={} for p in passwds: res=hashlib.md5(p.encode('utf-8')) dic[p]=res.hexdigest() # 模拟撞库得到密码 for k,v in dic.items(): if v == cryptograph: print('撞库成功,明文密码是:%s' %k) break

提升撞库的成本=>密码加盐

import hashlib m=hashlib.md5() m.update('天王'.encode('utf-8')) m.update('alex3714'.encode('utf-8')) m.update('盖地虎'.encode('utf-8')) print(m.hexdigest()) 4、subprocess模块

subprocess使用当前系统默认编码,得到结果为bytes类型,在windows下需要用gbk解码

import subprocess obj=subprocess.Popen('echo 123 ; ls / ; ls /root',shell=True, stdout=subprocess.PIPE, #正确的管道 stderr=subprocess.PIPE, #错误的管道 ) # print(obj) # res=obj.stdout.read() # print(res.decode('utf-8')) err_res=obj.stderr.read() print(err_res.decode('gbk')) # windows下需要用gbk解码mac、linux用utf-8解码

到此这篇关于Python hashlib模块与subprocess模块使用详细介绍的文章就介绍到这了,更多相关Python hashlib模块内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



subprocess Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号