Python hashlib模块
hashlib模块
- 用于加密相关的操作,代替了md5模块和sha模块
- 主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
# md5废弃
import md5
hash = md5.new()
hash.update('admin')
print hash.hexdigest()
# sha 废弃
import sha
hash = sha.new()
hash.update('admin')
print hash.hexdigest()
hashlib加密
import hashlib
# ######## md5 ########
hash = hashlib.md5()
hash.update("evescn".encode('utf-8'))
print(hash.hexdigest())
# ######## sha1 ########
hash = hashlib.sha1()
hash.update("evescn".encode('utf-8'))
print(hash.hexdigest())
# ######## sha256 ########
hash = hashlib.sha256()
hash.update("evescn".encode('utf-8'))
print(hash.hexdigest())
# ######## sha384 ########
hash = hashlib.sha384()
hash.update("evescn".encode('utf-8'))
print(hash.hexdigest())
# ######## sha512 ########
hash = hashlib.sha512()
hash.update("evescn".encode('utf-8'))
print(hash.hexdigest())
# 输入结果:
48fc6919f119605c4064ea7d81ac6b34
f2e9afa0fbde5b05a444f7bc75d6695fbb84b8d0
05406e832ad8456184cd36fd3bd88dd1fff2392eb873a1fe7aff9e6332bd7a35
3e8027e7a2aa54ba604fbe66f853212f23b4fbae80883972fc8a24a92c04b9cb7c3ee01840ccb98f5c3edf897d15f9fa
e7931c755048f72babc0b6f016c1581a51c4af7d3bef64a1c21156866a68cccf37d6914bacde9f0d7b83e328bc2025847f6754b6eec57be91f06e6ad5ca17d1b
python3,2种加密书写格式
import hashlib
# ######## md5 ########
hash = hashlib.md5()
hash.update("evescn".encode('utf-8'))
print(hash.hexdigest())
hash = hashlib.md5()
hash.update(b"evescn")
print(hash.hexdigest())
# 输入结果:
48fc6919f119605c4064ea7d81ac6b34
48fc6919f119605c4064ea7d81ac6b34
以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。
import hashlib
# ######## md5 ########
hash = hashlib.md5("evescn".encode('utf-8'))
hash.update("evescn".encode('utf-8'))
print(hash.hexdigest())
# 输入结果:
d6fd21d81a89fa360fe2ee37c6417b6f
python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密
import hmac
h = hmac.new(b"evescn")
h.update(b"evescn")
print(h.hexdigest())
# 输入结果:
bd4e50d0a9c686fad0a0be089ba918be
标签:########,hashlib,hash,Python,hexdigest,模块,print,md5
From: https://www.cnblogs.com/evescn/p/17501337.html