hash_hmac

sha256

PHP

//1.待加密数据 
$params                =    [
    'time'            =>    123,
    'nonce'            =>    '123',
    'project_id'    =>    123,
    'messages'        =>    '123',
];

//2.按照键值升序排列,用key=value形式组装,然后用&拼接
ksort($params);
$connects    =    [];
foreach ($params as $key => $value){
    array_push($connects, sprintf('%s=%s', $key, $value));
}
$connects    =   implode('&', $connects);

echo $connects;//messages=123&nonce=123&project_id=123&time=123

//3.hash_hmac sha256 加密,获取sign值
$key                =    '123';
$signStr            =    $connects;
$sign                =    hash_hmac('sha256', $signStr, $key);
echo $sign;//f2dfd49543055acd094b12d676af39687cc7e6f494956322a2f4d03870aaa44f

Go


func hashHmac(data string, key string) string {
    hash:= hmac.New(sha256.New, []byte(key))
    hash.Write([]byte(data))
    return hex.EncodeToString(hash.Sum([]byte("")))
}

Python


import hmac

def __hmacSha256(self, data, key):
    signature   =   hmac.new(key.encode('utf-8'), data.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()
    return signature

results matching ""

    No results matching ""