|
import hashlib |
|
import time |
|
import uuid |
|
|
|
''' |
|
添加鉴权相关参数 - |
|
appKey : 应用ID |
|
salt : 随机值 |
|
curtime : 当前时间戳(秒) |
|
signType : 签名版本 |
|
sign : 请求签名 |
|
|
|
@param appKey 您的应用ID |
|
@param appSecret 您的应用密钥 |
|
@param paramsMap 请求参数表 |
|
''' |
|
def addAuthParams(appKey, appSecret, params): |
|
salt = str(uuid.uuid1()) |
|
curtime = str(int(time.time())) |
|
sign = calculateSign(appKey, appSecret, salt, curtime) |
|
params['appKey'] = appKey |
|
params['salt'] = salt |
|
params['curtime'] = curtime |
|
params['signType'] = 'v4' |
|
params['sign'] = sign |
|
|
|
|
|
''' |
|
计算鉴权签名 - |
|
计算方式 : sign = sha256(appKey + input(q) + salt + curtime + appSecret) |
|
@param appKey 您的应用ID |
|
@param appSecret 您的应用密钥 |
|
@param salt 随机值 |
|
@param curtime 当前时间戳(秒) |
|
@return 鉴权签名sign |
|
''' |
|
def calculateSign(appKey, appSecret, salt, curtime): |
|
strSrc = appKey + salt + curtime + appSecret |
|
return encrypt(strSrc) |
|
|
|
|
|
def encrypt(strSrc): |
|
hash_algorithm = hashlib.sha256() |
|
hash_algorithm.update(strSrc.encode('utf-8')) |
|
return hash_algorithm.hexdigest() |
|
|