用Python做哈希之HMAC_SHA*_BASE64

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

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

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

用Python做哈希之HMAC_SHA*_BASE64_第1张图片

python代码:

import hmac
import base64
import hashlib
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()

def sha256_base64(message):
    hash_str = hashlib.sha256(message.encode())
    sig = base64.b64encode(hash_str.digest()).decode()
    print("length of sha256_base64 is ", len(sig))
    return sig

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))
    elif hash_method == "hash256_base64":
        print("hash256 base64 is ",sha256_base64(test_str))

通过修改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=
length of sha256_base64 is  44
hash256 base64 is  1yxVytzF1ng6H8N2+d5gWO05RUDaVY0SxqdN64D6kPM=

注意:

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

你可能感兴趣的:(code,design,skills,python,哈希算法,vscode,hmac-sha1,base64)