繁体   English   中英

如何将 php hash_hmac 转换为 NodeJS

[英]How to convert php hash_hmac to NodeJS

我需要将 php hash_hmac 转换为 node.js。 我找到了我尝试过的类似问题和答案,但我仍然收到 { data: '', error: 'HMAC not sent', success: false }

const params = {
method : "get_issues",
ts : time.toString(),
field_1_name : "id",
field_1_value : 123456,
sort : "status ASC, created DESC"
}

php:

 $mac = hash_hmac("sha512", mac_build_query($params), $PRIVATE_KEY); 

node.js:

let mac = crypto.createHmac("sha512", mac_build_query(params)).update(PRIVATE_KEY).digest().toString('base64') //('base64') //hex

在 PHP 中:

hash_hmac ( string $algo, string $data, string $key [, bool $raw_output = FALSE ] ): string

例子:

hash_hmac($algo,$data,$key) // return hex string 

在节点中: createHmac(algo, key).update(data).digest(encoding)

例子:

 crypto.createHmac('sha512', key).update(data).digest('hex')

注意:键和数据应该是一个字符串。

如果数据是 object 然后转换为字符串:

let data_json_string = JSON.stringify(data);

Output: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM