Last active
April 20, 2017 09:16
-
-
Save taojy123/2683c86f9e016c455c1b9d3358e11293 to your computer and use it in GitHub Desktop.
请求参数 RSA 签名
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_rsa_sign(data, private_key=PRIVATE_KEY): | |
# pip install pyopenssl | |
from OpenSSL.crypto import load_privatekey, FILETYPE_PEM, sign | |
import base64 | |
keys = sorted(data) | |
kstr = '' | |
for key in keys: | |
kstr += '&%s=%s' % (key, data[key]) | |
kstr = kstr.strip('&') | |
assert '---BEGIN RSA PRIVATE KEY---' in private_key | |
key = load_privatekey(FILETYPE_PEM, private_key) | |
d = sign(key, kstr, 'sha1') # d为经过SHA1算法进行摘要、使用私钥进行签名之后的数据 | |
b = base64.b64encode(d) # 将d转换为BASE64的格式 | |
return b | |
# 另一种方法 | |
def get_rsa_sign2(data, private_key=PRIVATE_KEY): | |
# pip install pycrypto | |
from Crypto.PublicKey import RSA | |
from Crypto.Signature import PKCS1_v1_5 | |
from Crypto.Hash import SHA | |
import base64 | |
keys = sorted(data) | |
kstr = '' | |
for key in keys: | |
kstr += '&%s=%s' % (key, data[key]) | |
kstr = kstr.strip('&') | |
assert '---BEGIN RSA PRIVATE KEY---' in private_key | |
key = RSA.importKey(private_key) | |
h = SHA.new(kstr) | |
signer = PKCS1_v1_5.new(key) | |
d = signer.sign(h) # d为经过SHA1算法进行摘要、使用私钥进行签名之后的数据 | |
b = base64.b64encode(d) # 将d转换为BASE64的格式 | |
return b | |
data = { | |
'apps_id': 'deb49cceb081467ba2667d1dbfb47b67', | |
'out_trade_no': datetime.datetime.now().strftime('%Y%m%d%H%M%S'), | |
'mer_id': '17033012353379798', | |
'total_fee': '100', | |
'subject': '打赏', | |
'body': '打赏 ishows', | |
'notify_url': 'http://ishows.sinaapp.com/pay_webhooks/', | |
'return_url': 'http://ishows.sinaapp.com/webhooks/', | |
'extra': 'test123', | |
} | |
sign = get_rsa_sign(data) | |
data['sign_type'] = 'RSA' | |
data['sign'] = sign | |
url = 'https://www.mustpay.com.cn/service/order/saveOrder' | |
import requests | |
result = requests.post(url, data=data).json() | |
# req = urllib2.Request(url) | |
# data = urllib.urlencode(data) | |
# response = req.open(req, data) | |
# result = response.read() | |
# result = json.loads(result) | |
print result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment