栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

用Python做哈希之HMAC

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Python做哈希之HMAC

在很多场景下,需要生成难以猜测的字符串,会采用选取一个key,将原code用hmac sha1制作成一个160比特的哈希并base64编码的形式。

这里便是使用此功能的python代码。python版本3.9。

推荐使用VSCODE调试python,可以一键安装python

python代码:

import hmac
import base64
from hashlib import *

def hash_hmac_sha1_base64(key, code, sha1):
    hmac_code = hmac.new(key.encode(), code.encode(), sha1).digest()
    print("length of hmac_sha1 is ", len(hmac_code))
    return base64.b64encode(hmac_code).decode()

def hash_hmac_sha1(key, code, sha1):
    hmac_code = hmac.new(key.encode(), code.encode(), sha1)
    return hmac_code.hexdigest()

def hash_hmac_sha256_base64(key, code, sha256):
    hmac_code = hmac.new(key.encode(), code.encode(), sha256).digest()
    print("length of hmac_sha256 is ", len(hmac_code))
    return base64.b64encode(hmac_code).decode()

def hash_hmac_sha256(key, code, sha256):
    hmac_code = hmac.new(key.encode(), code.encode(), sha256)
    return hmac_code.hexdigest()

def hash_hmac_sha3_256_base64(key, code, sha3_256):
    hmac_code = hmac.new(key.encode(), code.encode(), sha3_256).digest()
    print("length of hmac_sha3_256 is ", len(hmac_code))
    return base64.b64encode(hmac_code).decode()

def hash_hmac_sha3_256(key, code, sha3_256):
    hmac_code = hmac.new(key.encode(), code.encode(), sha3_256)
    return hmac_code.hexdigest()

if __name__ == '__main__':
    code = "code"
    key = "key"
    hash_method = "hash_hmac_sha3_256"
    if hash_method == "hash_hmac_sha1":
        print("hash hmac sha1 is ", hash_hmac_sha1(key, code, sha1))   
        print("hash hmac sha1 base64 is ", hash_hmac_sha1_base64(key, code, sha1))
    elif hash_method == "hash_hmac_sha256":
        print("hash hmac sha256 is ", hash_hmac_sha256(key, code, sha256))   
        print("hash hmac sha256 base64 is ", hash_hmac_sha256_base64(key, code, sha256))
    elif hash_method == "hash_hmac_sha3_256":
        print("hash hmac sha3_256 is ", hash_hmac_sha3_256(key, code, sha3_256))   
        print("hash hmac sha3_256 base64 is ", hash_hmac_sha3_256_base64(key, code, sha3_256))

通过修改hash_method,获取不同哈希的执行结果

hash hmac sha1 is  c3c3e6200cd3230c93dd01c9f5207472b2e035ba
length of hmac_sha1 is  20
hash hmac sha1 base64 is  w8PmIAzTIwyT3QHJ9SB0crLgNbo=
ash hmac sha3_256 is  28a711129ee186e7783d2aa5def34f9e73fc7a178126a38c3a3042c4d85df5bf
length of hmac_sha3_256 is  32
hash hmac sha3_256 base64 is  KKcREp7hhud4PSql3vNPnnP8eheBJqOMOjBCxNhd9b8=
hash hmac sha3_256 is  d24c72ea6457dfeaeb41d1f810b1aee614a88f496e064c418c8edc2928382894
length of hmac_sha3_256 is  32
hash hmac sha3_256 base64 is  0kxy6mRX3+rrQdH4ELGu5hSoj0luBkxBjI7cKSg4KJQ=

注意:

不可以直接拿在线hmac-sha*在线工具计算出结果,以hmac-sha1为例,计算后把结果的40个16进制数复制到在线base64计算工具中直接计算,这样会把40个16进制数直接当成40BYTE的字符串去进行base64计算,导致计算结果长度是正确结果的两倍长。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/349739.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号