import json
import hmac
import time
import datetime
import random
h_sk = "63QhvMPaWCdyARXhXxkfMQ=="
# 16进制本地时间戳
now_tm = (datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
time_array = time.strptime(now_tm, '%Y%m%d%H%M%S')
stamp_tm = int(time.mktime(time_array))
c_tm = hex(stamp_tm)[2:]
tt = str(c_tm).upper()
# 随机16进制字符串
def random_hex(length):
result = hex(random.randint(0,16**length)).replace('0x','').upper()
if(len(result)<length):
result = '0'*(length-len(result))+result
return result.upper()
result =random_hex(length=16)
url = 'xxx'
body = {
"zz" : "xxx"
}
def body_gene_token(num_hex, time_hex, url, body, sk):
# post请求
ck = hmac.new(sk.encode(), num_hex.encode(), 'sha256')
token_str = url + time_hex + json.dumps(body, separators=(",", ":"))
token = hmac.new(ck.hexdigest().encode(), token_str.encode(), 'sha256')
huawei_token = num_hex + "|" + time_hex + "|" + token.hexdigest().upper()
return h_token
def nobody_gene_token(num_hex, time_hex, url, sk):
# get请求
ck = hmac.new(sk.encode(), num_hex.encode(), 'sha256')
token_str = url + time_hex
token = hmac.new(ck.hexdigest().encode(), token_str.encode(), 'sha256')
ibs_token = num_hex + "|" + time_hex + "|" + token.hexdigest().upper()
return ibs_token
if __name__ == '__main__':
body_gene_token(num_hex=result,time_hex=tt,url=url,body=body,sk=h_sk)
nobody_gene_token(num_hex=result,time_hex=tt,url=url,sk=h_sk)