【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"))
复制代码

 

  

执行结果:

 

 

 

 

 

 

官方文档:https://docs.python.org/zh-cn/3/library/hmac.html

posted @   Phoenixy  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源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 的关联
点击右上角即可分享
微信分享提示