python中的hash_hmac sha512身份验证 [英] hash_hmac sha512 authentication in python

查看:382
本文介绍了python中的hash_hmac sha512身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为以下程序编写python身份验证机器人: https://comkort.com/page/private_api

I'm trying to write python authentication bot for: https://comkort.com/page/private_api

没有完整的php示例.我猜有人可以把它放在这里.

There is no full php example. I'm guess somebody could put it here.

只有一小段php代码:

There is only snippet of php code:

$query_string = http_build_query($_POST, '', '&');
$ethalon_sign = hash_hmac("sha512", $query_string, $api_secret_key);

如何使用hash_hmac sha512在python上编写身份验证?

How to write authentication on python with hash_hmac sha512 ?

我要提取我的open orders POST https://api.comkort.com/v1/private/order/list.

我当前的变体是:

import hashlib
import hmac
import requests
import time

privateKey = b'myprivatekey'
publicKey = 'my public key'

url = 'https://api.comkort.com/v1/private/order/list'
tosign = b'market_alias=doge_ltc'
signing = hmac.new( privateKey , tosign, hashlib.sha512 )
headers = {'sign': signing.digest(), "apikey": publicKey, "nonce": int( time.time() ) }

r = requests.get(url, headers=headers)
print r.text

我被抓住了

{"code":401,"error":"Invalid sign","field":"sign"}

可能是hexdigest()而不是digest()?我不知道,每次遇到一个错误:"Invalid sign"时,我都在玩这个b前缀和不同的hmac输出选项.

May be hexdigest() instead digest()? I don't know, I'm playing around this b prefix and different output hmac's options, everytime I'm caught one error: "Invalid sign".

相关: Python中的HMAC签名请求

推荐答案

如果有人感兴趣,我会自己解决.

If somebody interesting, I've solve this by myself.

#!/usr/bin/python

import hashlib
import hmac
import requests
import time

apikey = '';
apisecret = '';

def request_comkort( url, payload ):
        tosign = "&".join( [i + '=' + payload[i] for i in payload] )
        sign = hmac.new( apisecret, tosign , hashlib.sha512);
        headers = {'sign': sign.hexdigest(), 'nonce': int( time.time() ), 'apikey': apikey }
        r = requests.post(url, data=payload, headers=headers)
        return r.text

# Get balance
print request_comkort( "https://api.comkort.com/v1/private/user/balance";, {} )
# Get Open Orders 
print request_comkort( "https://api.comkort.com/v1/private/order/list";, {'market_alias': "DOGE_LTC" } )
# Make Ask
print request_comkort( "https://api.comkort.com/v1/private/order/sell";, { 'market_alias':"HTML_DOGE", "amount": "1000", "price": "123123" } )
# Cancel order
print request_comkort( "https://api.comkort.com/v1/private/order/cancel";, { 'order_id': 10943 } )

这篇关于python中的hash_hmac sha512身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆