import hashlib
import json
import base64
import sys
from collections import OrderedDict
from Crypto.PublicKey import RSA
from Crypto.Hash import MD5, SHA1, SHA256
from Crypto.Signature import PKCS1_v1_5 as Signature_PKC

def main():
    # 从标准输入读取JSON字符串
    input_json = sys.stdin.read().strip()
    # 解析JSON数据
    input_data = json.loads(input_json)
    # 从输入数据中获取参数
    req_body_data = input_data["reqBodyData"]
    timestamp = input_data["timestamp"]
    appId = input_data["appId"]
    appKey = input_data["appKey"]

    # 按照阿拉伯字符排序
    jsonstr = json.dumps(req_body_data, sort_keys=True,separators=(',',':'), ensure_ascii=False)
    data = dict()
    data["appId"] = appId
    data["timestamp"] = timestamp
    data["bizData"] = jsonstr

    #计算md5值
    m = hashlib.md5()
    m.update(jsonstr.encode('utf-8'))
    # 二进制数据字符串值
    md5_str = m.hexdigest()
    sign_str = '%s%s%s%s' % (jsonstr, md5_str, appId, timestamp)
    # 打印签名值
    private_keyBytes = base64.b64decode(appKey)
    rsakey = RSA.importKey(private_keyBytes)
    signer = Signature_PKC.new(rsakey)
    # 根据SHA256算法处理签名内容data
    sha_data = SHA1.new(sign_str.encode("utf-8"))
    #sha_data = SHA1.new(sign_str)
    # 私钥进行签名
    signature = base64.b64encode(signer.sign(sha_data))

    # 输出签名结果
    print(signature)


if __name__ == "__main__":
    main()