PHP / Javascript / JQuery - base64 sha256编码 [英] PHP / Javascript / JQuery - base64 sha256 encoding
问题描述
我正在尝试将API集成的PHP示例移植到Javascript / JQuery。在PHP中,使用以下代码创建加密字符串:
I'm trying to port a PHP example of an API integration to Javascript / JQuery. In PHP, an encrypted string is created using the following code:
$sig = base64_encode(hash_hmac('sha256', $sign, $this->secretAccessKey, true)
其功能记录在此处:
http://php.net/manual /en/function.hash-hmac.php
http://us.php.net/base64_encode
在Javascript中,我使用JQuery的加密来执行HMAC:
In Javascript, I'm using JQuery's crypto to do the HMAC piece:
http://code.google.com/p/crypto-js/#HMAC-SHA256
我正在试图弄清楚我是否还需要做base64编码,因为它似乎已经在base64中了。这就是我正在运行的代码:
and I'm trying to figure out if I also need to do a base64 encode, as it seems it is already in base64. This is the code I'm currently running:
var sigHash = Crypto.HMAC(Crypto.SHA256, sign, accessKey);
这是正确的吗?我将如何创建?一个javascript / jquery等价于上面的PHP函数?
Is this correct? How would I create a javascript / jquery equivalent of the above PHP function?
推荐答案
HMAC是一个标准。 SHA-256也是如此。所以他们的输出,无论哪种实现,都必须相同。
HMAC is a standard. So is SHA-256. So their outputs, regardless of which implementation, has to be the same.
Base64编码只能存在差异。通常,非字母数字字符是 +
和 /
,但你不能指望它。我已经检查过,两个实现都使用相同的非字母数字字符。
There could only be differences in the Base64 encoding. Normally, the non-alphanumeric characters are +
and /
, but you cannot count on that. I've checked, and both implementations use the same non-alphanumeric characters.
但是,你仍然应该手动检查几千个字符串。 PHP中的实现经过了充分测试。但我不知道jQuery中的实现是否也是如此...
However, you should still "manually" check a few thousand strings. The implementation in PHP is well tested. But I do not know if the same is true for the implementation in jQuery...
Base64编码输出的语法是:
The syntax for Base64 encoded output is:
Crypto.util.bytesToBase64(
Crypto.HMAC(Crypto.SHA256, sign, accessKey, { asBytes: true })
);
这篇关于PHP / Javascript / JQuery - base64 sha256编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!