NodeJS返回来自HMAC的其他二进制结果,而不是php

我正在windows上运行node.js和php,并在node.js中使用包含的encryption模块。

PHP脚本:

hash_hmac("sha256", "foo", "bar", true) // the true enables binary output 

输出:

¶y3!è¬╝♂ï►ó│Ñ├Fä╚┘CA╝±G6▄rp¸t↑Q

Node.js脚本:

 crypto.createHmac("sha256", "bar").update("foo").digest("binary"); 

输出:

¶y3!?ª¼♂?►¢³¥AF?ÈÙCA¼ñG6Ürp+ T↑Q

我也想知道为什么一些数字是相同的,但其他一些没有。


我也尝试得到hex而不是二进制结果,他们都输出相同

 hash_hmac("sha256", "foo", "bar", false); // false outputs hex data crypto.createHmac("sha256", "bar").update("foo").digest("hex"); // notice "hex" 

这不是一个解决scheme,因为我无法将hex数据转换为二进制:

 var hmac = crypto.createHmac("sha256", "bar").update("foo").digest("hex"); var binary = new Buffer(hmac, "hex"); 

可变的binary输出:

¶y3!???♂?►???? F ??? ?? CA G6?RP?吨↑Q