Neurohazard
暮雲煙月,皓首窮經;森羅萬象,如是我聞。

Python 数字签名 demo

wpadmin~September 19, 2018 /InfoSec

Python 数字签名 demo sha1 digital signature

Python 数字签名 demo

正文

仅作示例,代码中私钥无效。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time    : 2018/9/18 22:53
# Author  : BLKStone
# Site    : http://wp.blkstone.me
# File    : rong360.py
# Software: PyCharm

import requests
import hashlib
import hmac
import base64
import time
import json
import collections

def test1():

    # https://github.com/Mrliutaotao/local-project/blob/667280433fe44eea1f84914116662ef2daa9e9e7/local-project/src/main/java/learn/httpSender/test/ClientManager.java

    # HTTPS请求的方法不正确
    # print("HTTPS\u8bf7\u6c42\u7684\u65b9\u6cd5\u4e0d\u6b63\u786e".decode('unicode_escape'))
    # print("\u4f20\u5165\u53c2\u6570\u4e0d\u5b8c\u6574\uff0c\u7f3a\u5c11method".decode('unicode_escape'))

    print("HTTPS\u8bf7\u6c42\u7684\u65b9\u6cd5\u4e0d\u6b63\u786e")
    print("\u4f20\u5165\u53c2\u6570\u4e0d\u5b8c\u6574\uff0c\u7f3a\u5c11method")
    print("\u4f20\u5165\u53c2\u6570\u4e0d\u5b8c\u6574\uff0c\u7f3a\u5c11biz_data")
    print("\u9a8c\u8bc1\u7b7e\u540d\u5931\u8d25")

    # 测试环境时,请在hosts文件中添加:  59.151.86.29 openapi.rong360.com
    # 联调时,请机构修改为测试环境地址

    # 融360开放平台分配的商户id 值与biz_data中merchant_id一样(注意修改CrawlerRequest中的merchantId的值)

    url = "https://openapi.rong360.com/gateway"
    app_id = "3000002"
    format = "json"

    private_key = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANPKNNkvHFg9RWDg\n" +\
                 "lkZy96XiRiP2dZIRSDJRL2gMi4iFVhlwdvltKus3E4R3vhNNwQJpcuiJP0KYSaHy\n" +\
                 "d8C2RNcp/dr7XZrCP3MmhbC8FpSAPpDbWzDh34LYxn8Gwkk+GXq3csJ5+VszyNQy\n" +\
                 "LkjQ/V+5A6CBMjjMlOk0NU6wazO5AgMBAAECgYAqN7yhLorv7AbQcPSDxpcnMhvZ\n" +\
                 "P1/gZiGGJjjU/Oszo6CuIYUV43USvj/dwCDbqDw5RtvpDrLVi5Xh+nt0puDBxm/5\n" +\
                 "BcaEhZ/wZO0BsUO3td7irrabxvdOZXXhePf0CNHy7WYe9gQOOdVkJRsn18SYjgFJ\n" +\
                 "5J5DGETF9wtqrI9CgQJBAPGS8kwEQwN1BG6eAofetEr3mlr0+NfnmoKDtKTjWB0n\n" +\
                 "CB/lQQ2AnV0+db3+i3rVjViiTcXoqw1Ovk597N8V4G0CQQDgb++gdxVaDyKXWTTm\n" +\
                 "g0LwpBJL2258CLu4yIiQFs9AfyaSrCV6NT+upqBnWGbmm4Jf363aozrYrEHeAfgo\n" +\
                 "TQj9AkBC8ValjBTJ83Zr9Ot8nVFW6PBkPjhrFCoz+q1nd/yl73gH5q61QCvbeACG\n" +\
                 "yu/59Q27PxbQPh6QjH6eH7UxSM2tAkBAnlna1I50OIlYFBoUCFTcnhCagJol4gnS\n" +\
                 "YQJYogiX5EQB1MiRkAU+zsC+IIi3+qwl2Gvg2EBYI/hu6Bg/2jYtAkEA6YApkkdB\n" +\
                 "sZmeRprs9ZQAZShMBj06EuK6mFdoJpIatZOxPxw/R12MT4IbdD232ajpxOAViFVI\n" +\
                 "GhocDo0Q3YRDYg=="

    execute(app_id, private_key)


def execute(app_id, private_key, transfer_format='json'):
    url = "https://openapi.rong360.com/gateway"
    version = "1.0.0"
    sign_type = "RSA"

    params = collections.OrderedDict()
    params['app_id'] = app_id
    params['version'] = version
    params['sign_type'] = sign_type
    params['format'] = transfer_format
    params['timestamp'] = str(int(time.time()))
    params['method'] = 'get'
    params['biz_data'] = app_id
    params_string = json.dumps(params)
    print(params_string)

    sign = sha1_digest(params_string, private_key)
    params['sign'] = sign
    params_string = json.dumps(params)
    print(params_string)

    r = requests.post(url, params)
    print(r.text)


# https://gist.github.com/heskyji/5167567b64cb92a910a3
def sha1_digest(message, private_key):
    key = bytes(private_key, 'UTF-8')
    message = bytes(message, 'UTF-8')

    digester = hmac.new(key, message, hashlib.sha1)
    signature = digester.digest()
    base64_signature = base64.standard_b64encode(signature)
    return  str(base64_signature, 'UTF-8')


if __name__ == '__main__':
    test1()

Leave a Reply

Your email address will not be published. Required fields are marked *