hashlib模块
用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
1 2 3 4 5 6 7 8 9 10 | import hashlib m = hashlib.md5() m.update(b "Hello" ) m.update(b "It's me" ) print (m.digest()) m.update(b "It's been a long time since last time we ..." ) print (m.digest()) #2进制格式hash print ( len (m.hexdigest())) #16进制格式hash |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import hashlib hash = hashlib.md5() hash .update(b 'admin' ) print ( hash .hexdigest()) hash = hashlib.sha1() hash .update(b 'admin' ) print ( hash .hexdigest()) hash = hashlib.sha256() hash .update(b 'admin' ) print ( hash .hexdigest()) hash = hashlib.sha384() hash .update(b 'admin' ) print ( hash .hexdigest()) hash = hashlib.sha512() hash .update(b 'admin' ) print ( hash .hexdigest()) |
python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密
1 2 3 4 | import hmac h = hmac.new(b 'wueiqi' ) h.update(b 'hellowo' ) print (h.hexdigest()) |