zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python实现SHA256

2023-03-20 14:57:04 时间

from hashlib import sha256
import hmac

def get_sign(key, data):

#sha256加密有2种
# hsobj = sha256(key.encode("utf-8"))
# hsobj.update(data.encode("utf-8"))
# print(hsobj.hexdigest().upper())

data = data.encode('utf-8')
print(hmac.new(key.encode('utf-8'), data, digestmod=sha256).hexdigest().upper())

key=‘1546084445901’
data=‘testappSecret’
#get_sign(key,data)
get_sign(data,key)
注意点:

1、key、data参数不要反了;

2、hexdigest表示加密字符串转化成16进制;

3、python3里sha256有两种,hmac.new的结果才是符合要求的。

原文链接:https://blog.csdn.net/DH2442897094/article/details/112224687