标签:python import result 加密 admin
import hashlib
#obj = hashlib.md5(bytes(‘‘,encoding=‘utf-8‘))
obj=hashlib.md5(bytes(‘123‘,encoding=‘utf-8‘))
result = obj.hexdigest()
print(result)
用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import hashlib # ######## md5 ######## hash = hashlib.md5() # help(hash.update) hash .update(bytes( ‘admin‘ , encoding = ‘utf-8‘ )) print ( hash .hexdigest()) print ( hash .digest()) ######## sha1 ######## hash = hashlib.sha1() hash .update(bytes( ‘admin‘ , encoding = ‘utf-8‘ )) print ( hash .hexdigest()) # ######## sha256 ######## hash = hashlib.sha256() hash .update(bytes( ‘admin‘ , encoding = ‘utf-8‘ )) print ( hash .hexdigest()) # ######## sha384 ######## hash = hashlib.sha384() hash .update(bytes( ‘admin‘ , encoding = ‘utf-8‘ )) print ( hash .hexdigest()) # ######## sha512 ######## hash = hashlib.sha512() hash .update(bytes( ‘admin‘ , encoding = ‘utf-8‘ )) print ( hash .hexdigest()) |
以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。
1 2 3 4 5 6 7 | import hashlib # ######## md5 ######## hash = hashlib.md5(bytes( ‘898oaFs09f‘ ,encoding = "utf-8" )) hash .update(bytes( ‘admin‘ ,encoding = "utf-8" )) print ( hash .hexdigest()) |
python内置还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密
1 2 3 4 5 | import hmac h = hmac.new(bytes( ‘898oaFs09f‘ ,encoding = "utf-8" )) h.update(bytes( ‘admin‘ ,encoding = "utf-8" )) print (h.hexdigest()) |
标签:python import result 加密 admin
原文地址:http://liulidong.blog.51cto.com/9439411/1790475