Python基础(23)之hashlib模块

Python基础(23)之hashlib模块

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

md5 和 sha 加密

import hashlib

str = '123'
md5_helper = hashlib.md5()
md5_helper.update(bytes(str, 'utf-8'))
print(md5_helper.hexdigest())  # 202cb962ac59075b964b07152d234b70

sha_helper = hashlib.sha1()
sha_helper.update(bytes(str, 'utf-8'))
print(sha_helper.hexdigest())  # 40bd001563085fc35165329ea1ff5c5ecbdbbeef

加盐

import hashlib

str = '123'
md5_helper = hashlib.md5()
md5_helper.update(bytes(str, 'utf-8'))
print(md5_helper.hexdigest())  # 202cb962ac59075b964b07152d234b70
# 加盐
md5_salt_helper = hashlib.md5("salt".encode("utf8"))
md5_salt_helper.update(bytes(str, 'utf-8'))
print(md5_salt_helper.hexdigest())  # 8c4fb7bf681156b52fea93442c7dffc9

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/python-base-23.html

Buy me a cup of coffee ☕.