微信支付 jsapi v3签名

import json
import os
import time
import random
import string
import base64
# 创建订单
import requests
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import PKCS1_v1_5
from Cryptodome.Hash import SHA256

import config


class ToSign:
    timestamp = "%.f" % time.time()  # "%.f" % time.time()  # 时间戳
    nonce_str = "".join(random.sample(string.ascii_letters + string.digits, 16))  # 随机字符串

    @classmethod
    def set_sign_data(cls, method: str, url: str, body: dict):
        """设置默认数据 """
        cls.method = method
        cls.url = url
        cls.body = json.dumps(body) # 转换为json字符串

    @classmethod
    def sign_str(cls):
        """生成欲签名字符串"""
        return str("\n".join([cls.method, cls.url,
                              cls.timestamp, cls.nonce_str,
                              cls.body])+"\n")

    @classmethod
    def sign(cls, sign_str):
        """签名 """
        with open("./app/handler/wechat_app/apiclient_key.pem", 'r') as f:
            private_key = f.read()
        pkey = RSA.importKey(private_key)
        h = SHA256.new(sign_str.encode('utf-8'))
        signature = PKCS1_v1_5.new(pkey).sign(h)
        sign = base64.b64encode(signature).decode()
        return sign

    @classmethod
    def authorization_str(cls):
        sign_ = cls.sign(cls.sign_str())
        """拼接header authorization"""
        authorization = 'WECHATPAY2-SHA256-RSA2048 ' \
                        'mchid="{mchid}",' \
                        'nonce_str="{nonce_str}",' \
                        'signature="{sign}",' \
                        'timestamp="{timestamp}",' \
                        'serial_no="{serial_no}"'. \
            format(mchid="1609191198",
                   nonce_str=cls.nonce_str,
                   sign=sign_,
                   timestamp=cls.timestamp,
                   serial_no=config.serial_no
                   )
        return authorization

上一篇:INSERT INTO ON DUPLICATE KEY UPDATE


下一篇:Electron播放 RTMP流 实现