签名规则,原文排序,排完拼成key=value&key=value的字符串,再进行签名,签名完了,再把签名添加到请求体中一起发送
def broker_signature(self,data):
# 先排序
js = json.dumps(data, ensure_ascii=False,separators=(',',':'),sort_keys=True)
# 将 JSON 对象转换为 Python 字典
json_dict = json.loads(js)
items = sorted(json_dict.items())
# 在拼为新的字符串
result = ''
for key, value in items:
result += (str(key) + '=' + str(value)) + '&'
org_data = result[:-1]
#摘要处理原文
digest_data = SHA256.new(org_data.encode("utf8")).hexdigest()
d_data = SHA256.new(digest_data.encode('utf8'))
private_key = base.file_path + '/data/privatekey.pem'
with open(private_key,encoding='utf-8') as f:
key = f.read()
rsa_key = RSA.import_key(key)
#私钥签名
sign_pk = Sign_PK.new(rsa_key)
sign_data = sign_pk.sign(d_data)
#是否需要urlbase64
SHA256WithRSA_base64 = base64.b64encode(sign_data).decode()
#在原data里面新增一个signature的key-value
data.update({"signature":SHA256WithRSA_base64})
return data