import hmac
import base64
import datetime
from . import consts as c
[docs]
def sign(message, secretKey):
mac = hmac.new(bytes(secretKey, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
d = mac.digest()
return base64.b64encode(d)
[docs]
def pre_hash(timestamp, method, request_path, body,debug = True):
if debug == True:
print('body: ',body)
return str(timestamp) + str.upper(method) + request_path + body
[docs]
def parse_params_to_str(params):
url = '?'
for key, value in params.items():
if(value != ''):
url = url + str(key) + '=' + str(value) + '&'
url = url[0:-1]
#print('url:',url)
return url
[docs]
def get_timestamp():
now = datetime.datetime.utcnow()
t = now.isoformat("T", "milliseconds")
return t + "Z"
[docs]
def signature(timestamp, method, request_path, body, secret_key):
if str(body) == '{}' or str(body) == 'None':
body = ''
message = str(timestamp) + str.upper(method) + request_path + str(body)
mac = hmac.new(bytes(secret_key, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
d = mac.digest()
return base64.b64encode(d)