Particles

hashlib模块

#hashlib模块

  • 摘要算法,也叫做加密算法,或者是哈希算法,散列算法等等,做加密和校验使用

#md5 普通加密

#md5普通加密

import hashlib
md5 = hashlib.md5()
md5.update("LCh123456".encode("utf-8"))  #必须是bytes类型才能加密
print(md5.hexdigest())
>>>>d60861d4be2012e49709703fb7470a24
###相同的bytes数据的加密转换一定是相同的

#md5 + 固定加盐

md5 = hashlib.md5('要加的盐'.encode('utf-8'))  #'爸爸' 就是加的盐
md5.update("要加密的内容".encode("utf-8"))
print(md5.hexdigest())
>>>97d56646f57a6a24041f3c84a2eaab6e

#md5 + 动态的盐

username = input("请输入用户")
md5 = hashlib.md5(username[::2].encode("utf-8"))  #不同的用户加入不同的盐
md5.update("要加密的内容".encode("utf-8"))
print(md5.hexdigest())
###输入不同数据加不同的盐

#sha1,sha224,sha512等

#sha1普通加密

ret = hashlib.sha1()
ret.update("要加密的内容".encode("utf-8"))
print(ret.hexdigest())
>>>8046e81fa5904f03f9c3165aaf5b47ee1ae54d8b

#sha1 + 固定加盐

ret = hashlib.sha1(b'asdasd')
ret.update("要加密的内容".encode("utf-8"))
print(ret.hexdigest())
>>>a97c79a519bdc266f2c66986ce08b0c4b53692fc

posted @ 2020-01-08 14:52  Na_years  阅读(160)  评论(0编辑  收藏  举报