编程语言
首页 > 编程语言> > python按照ACCII排序,&拼接,使用sm3加密

python按照ACCII排序,&拼接,使用sm3加密

作者:互联网

def get_sm3(dateSign):
# data字段的值进行排序
l_list = []
l = dateSign[‘data’]
log.info(‘需要签名的报文:{0}’.format(dateSign))
# data按照顺序读取
for d in sorted(l):
m = ‘{0}{1}{2}{3}{4}{5}{6}{7}’.format(’"’, d, ‘"’, ‘:’, ‘"’, l[d], ‘"’, ‘,’)
l_list.append(m)
l_list_string = “”.join(l_list)
s_list = [] # 新建空列表,存储date的值
# 循环读取date中的值
for i in sorted(dateSign):
if i == ‘data’:
n = ‘{0}{1}’.format(i, ‘=’)
s_list.append(n)
zifu01 = ‘{’
s_list.append(zifu01)
s_list.append(l_list_string)
zifu02 = ‘}’
s_list.append(zifu02)
zifu = “&”
s_list.append(zifu)
else:
s = ‘{0}{1}{2}{3}’.format(i, ‘=’, dateSign[i], ‘&’)
s_list.append(s)
# 列表转换为str
s_list_string = “”.join(s_list)
signRegion_key = s_list_string + “key=C2C2F53F74BD23DE5242885762E99E9E”
# 单引号转换为双引号
signRegion_test = signRegion_key.replace(’’’, ‘"’)
# 空格替换
signRegion_test = signRegion_test.replace(’: ‘, ‘:’)
signRegion_test = signRegion_test.replace(’,}’, ‘}’)
signRegion = signRegion_test.replace(’, ', ‘,’)
log.info(‘待签名字符串:{0}’.format(signRegion))
sign = bytes(signRegion, encoding=“utf8”)
sign_data = sm3test.sm3_hash(sign)
log.info(‘签名结果:{0}’.format(sign_data))
return sign_data
data = {“app_type”:“0”,“appid”:“11”}
get_sm3(data)

标签:sm3,format,python,list,ACCII,test,signRegion,data,append
来源: https://blog.csdn.net/citrin/article/details/121957904