Python?hashlib模塊與subprocess模塊使用詳細介紹
1、什么是哈希hash
hash一類算法,該算法接受傳入的內(nèi)容,經(jīng)過運算得到一串hash值
hash值的特點:
- 只要傳入的內(nèi)容一樣,得到的hash值必然一樣
- 不能由hash值返解成內(nèi)容
- 不管傳入的內(nèi)容有多大,只要使用的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使用當前系統(tǒng)默認編碼,得到結(jié)果為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模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用eval函數(shù)執(zhí)行動態(tài)標表達式過程詳解
這篇文章主要介紹了Python使用eval函數(shù)執(zhí)行動態(tài)標表達式過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
將python字符串轉(zhuǎn)化成長表達式的函數(shù)eval實例
tensorflow2.0如何實現(xiàn)cnn的圖像識別
解決更新tensorflow后應用tensorboard報錯的問題
python基礎入門詳解(文件輸入/輸出 內(nèi)建類型 字典操作使用方法)

