sign

    def getSignStr(secretKey, signStr):
'''
https://cloud.tencent.com/document/product/570/13939
$secretKey = 'pxPgRWDbCy86ZYyqBTDk7WmeRZSmPco0';
$srcStr = 'GETdsa.api.qcloud.com/v2/index.php?Action=GetDsaHostList&Nonce=48059&SecretId=AKIDT8G5AsY1D3MChWooNq1rFSw1fyBVCX9D&SignatureMethod=HmacSHA256&Timestamp=1502197934&length=10&offset=0';
$signStr = base64_encode(hash_hmac('sha256', $srcStr, $secretKey, true));
echo $signStr;
oC20lImZgsEZYZqHYQnbvBxEkIFUxgoDhE3GkQA8Ax8=


https://www.php.net/manual/en/function.hash-hmac.php
hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : string
algo
Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..) See hash_hmac_algos() for a list of supported algorithms.

data
Message to be hashed.

key
Shared secret key used for generating the HMAC variant of the message digest.

raw_output
When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.


# https://github.com/TencentCloud/tencentcloud-sdk-python/blob/c53791afe6403c57a0cacbf9cb1512870c070ec4/tencentcloud/common/sign.py

:param strList:
:return:
'''
import hmac
import hashlib
import urllib.parse
import binascii

signStr = bytes(signStr, 'utf-8')
secretKey = bytes(secretKey, 'utf-8')
digestmod = hashlib.sha256
hashed = hmac.new(secretKey, signStr, digestmod)
base64 = binascii.b2a_base64(hashed.digest())[:-1]
base64 = base64.decode()
ret = urllib.parse.quote(base64, safe='')
return ret
posted @ 2019-10-15 20:49  papering  阅读(597)  评论(0编辑  收藏  举报