【Python】hmac模块_基于密钥的消息验证
HMAC算法可以用于验证信息的完整性,这些信息可能在应用之间或者网络间传递
1、SHA加密
# -*- coding:utf-8 -*- import hmac import hashlib class hmac_tools: def __init__(self): self.key = "a12345678" def sha512Encrypt(self, msg): h = hmac.new(self.key.encode("utf-8"), msg.encode("utf-8"), "sha512") return h.hexdigest() def sha384Encrypt(self, msg): h = hmac.new(self.key.encode("utf-8"), msg.encode("utf-8"), "sha384") return h.hexdigest() def sha256Encrypt(self, msg): h = hmac.new(self.key.encode("utf-8"), msg.encode("utf-8"), "sha256") return h.hexdigest() def sha1Encrypt(self, msg): h = hmac.new(self.key.encode("utf-8"), msg.encode("utf-8"), hashlib.sha1) return h.hexdigest() def md5Encrypt(self, msg): h = hmac.new(self.key.encode("utf-8"), msg.encode("utf-8"), "md5") return h.hexdigest() if __name__ == "__main__": """run""" print("sha512加密:", hmac_tools().sha512Encrypt("this is message")) print("sha384加密:", hmac_tools().sha384Encrypt("this is message")) print("sha256加密:", hmac_tools().sha256Encrypt("this is message")) print("sha1加密:", hmac_tools().sha1Encrypt("this is message")) print("md5加密:", hmac_tools().md5Encrypt("this is message"))
执行结果:
-------------------------------------------------------------------------------------
如果万事开头难 那请结局一定圆满 @ Phoenixy
-------------------------------------------------------------------------------------
分类:
Python
标签:
python 加解密算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2022-05-26 【idea】修改编辑项目JSP文件时,需要重新运行项目才能刷新
2022-05-26 【idea】创建JavaEE项目并配置Tomcat启动项目
2022-05-26 【idea】解除idea 与git 的关联