我在windows上运行node.js和php,并使用node.js.中包含的加密模块
Php脚本:
hash_hmac("sha256", "foo", "bar", true) // the true enables binary output
输出:
¶y3!╝♂ï►ó│ñ├Fä╚┘CA╝±G6▄rp↑Q
Node.js脚本:
crypto.createHmac("sha256", "bar").update("foo").digest("binary");
输出:
¶y3!?——♂?►¢³¥?加利福尼亚州¼ñG6Ürp÷t↑Q
我还想知道为什么有些数字是一样的,而另一些数字却不一样。
我还尝试获得十六进制而不是二进制结果,它们都输出相同的。
hash_hmac("sha256", "foo", "bar", false); // false outputs hex data
crypto.createHmac("sha256", "bar").update("foo").digest("hex"); // notice "hex"
这不是一个解决方案,因为我未能将十六进制数据转换为二进制:
var hmac = crypto.createHmac("sha256", "bar").update("foo").digest("hex");
var binary = new Buffer(hmac, "hex");
变量binary
输出:
¶y3!???♂?►????FCA??G6?rp?t↑Q
我在为OTP simplepay实现node-js解决方案时遇到了同样的问题。
PHP代码:
base64_encode(hash_hmac('SHA384', $text, trim($key), true));
JS代码:
function get_hash(key, text) {
const crypto = require("crypto");
const algorithm = "sha384";
var hmac = crypto.createHmac(algorithm, key).update(text);
return hmac.digest().toString('base64');
}
所以两者都注销/回显-给出相同的结果。在您的情况下,二进制输出将是:
crypto.createHmac("sha256", "bar").update("foo").digest().toString('binary');
但是,请记住,由于字符编码的原因,日志记录和回显二进制字符串会给出稍微不同的视图。你可以看到相同的,但也可以看到不同的字符。
PHP回波
,cAW'B��o��傱�@�Vlάf�R@y�,?0�^1=Y�����u2
和
节点控制台.log
,cAW'B
其实是一样的,只是看起来不一样。请参阅此github问题和addaleax的评论
calculateHmac(payload) {
const hmac = crypto.createHmac('sha256', KEY);
hmac.update(Buffer.from(payload).toString());
let bin = hmac.digest();
return Buffer.from(bin).toString('base64');
}