-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.py
49 lines (35 loc) · 1.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import hmac
import base64
import time
import datetime
import consts as c
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)
def pre_hash(timestamp, method, request_path, body):
return str(timestamp) + str.upper(method) + request_path + body
def get_header(api_key, sign, timestamp, passphrase):
header = dict()
header[c.CONTENT_TYPE] = c.APPLICATION_JSON
header[c.OK_ACCESS_KEY] = api_key
header[c.OK_ACCESS_SIGN] = sign
header[c.OK_ACCESS_TIMESTAMP] = str(timestamp)
header[c.OK_ACCESS_PASSPHRASE] = passphrase
return header
def parse_params_to_str(params):
url = '?'
for key, value in params.items():
url = url + str(key) + '=' + str(value) + '&'
return url[0:-1]
def get_timestamp():
now = datetime.datetime.now()
t = now.isoformat()
return t + "Z"
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)