python、微信V3版本,生成签名

签名串一共有五行,每一行为一个参数。行尾以 \n结束,包括最后一行。如果参数本身以\n结束,也需要附加一个\n。

HTTP请求方法\n
URL\n
时间戳\n
随机字符串\n
请求报文主体\n

def sign_str(method, url, timestamp, nonce_str, body):
    """
    拼接sign字符串
    """
    sign_list = [
        method,
        url,
        timestamp,
        nonce_str,
        body
    ]
    return '\n'.join(sign_list) + '\n'
#生成sign
def calculate_sign(body, method, url, timestamp, nonce_str):
    a = sign_str(method, url, timestamp, nonce_str, json.dumps(body))
    signer = pkcs1_15.new(RSA.importKey(open(r"apiclient_key.pem").read()))
    signature = signer.sign(SHA256.new(a.encode("utf-8")))
    sign = encodebytes(signature).decode("utf-8").replace("\n", "")
    return sign
posted @ 2021-03-17 11:17  做个笔记  阅读(289)  评论(0编辑  收藏  举报