簡單了解Python hashlib模塊
一.hashlib模塊
用于加密相關(guān)的操作,3.x里代替了md5模塊和sha模塊,主要提供
:SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法。
1.使用hashlib模塊進(jìn)行MD5加密。
import hashlibm = hashlib.md5()m.update(b'Hello')m.update(b'It’s me')print(m.hexdigest())m.update(b'It’s been a long time since last time we ...')print(m.digest())
注:hashlib.md5():創(chuàng)建一個md5加密參數(shù)。
注:變量.hexdigest():十六進(jìn)制加密。
注:變量.digest():二進(jìn)制加密
注:總共有32個值。
2.使用SHA1算法加密。
import hashlibs2 = hashlib.sha1()s2.update(b'abc')print(s2.hexdigest())
注:總共有38個值。
3.使用SHA256算法加密
s2 = hashlib.sha256()s2.update(b'abc')print(s2.hexdigest())print(len(s2.hexdigest()))
注:有64個加密數(shù)值。
4.使用SHA384算法加密。
import hashlibs2 = hashlib.sha384()s2.update(b'abc')print(s2.hexdigest())print(len(s2.hexdigest()))
注:有96個加密數(shù)值。
5.使用SHA512算法加密。
import hashlibs2 = hashlib.sha512()s2.update(b'abc')print(s2.hexdigest())print(len(s2.hexdigest()))
注:有128個加密數(shù)值。
二.加密算法進(jìn)階
import hmach = hmac.new(’wueiqi’)h.update(’hellowo’)print h.hexdigest()
注:相當(dāng)于加密后又進(jìn)行一層加密。雙層加密。
import hmach = hmac.new(b'xsk','y風(fēng)的風(fēng)x'.encode(encoding='utf-8'))print(h.digest())print(h.hexdigest())
注:雙層加密。
注:加入中文key需要,轉(zhuǎn)換字符類型。
以上就是簡單了解Python hashlib模塊的詳細(xì)內(nèi)容,更多關(guān)于Python hashlib模塊的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式2. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))3. python web框架的總結(jié)4. 詳解Python模塊化編程與裝飾器5. Python通過format函數(shù)格式化顯示值6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. python裝飾器三種裝飾模式的簡單分析8. Python如何進(jìn)行時間處理9. Python實(shí)現(xiàn)迪杰斯特拉算法過程解析10. python使用ctypes庫調(diào)用DLL動態(tài)鏈接庫
