PHP / Javascript / JQuery - base64 sha256 编码
我正在尝试将 API 集成的 PHP 示例移植到 Javascript / JQuery。在 PHP 中,使用以下代码创建加密字符串:
$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 部分:
http://code.google.com/p/crypto-js/#HMAC-SHA256
并且我试图弄清楚我是否还需要这样做一个base64编码,因为它似乎已经是base64了。这是我当前正在运行的代码:
var sigHash = Crypto.HMAC(Crypto.SHA256, sign, accessKey);
这是正确的吗?我如何创建与上述 PHP 函数等效的 javascript / jquery?
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)
whose functions are documented here:
http://php.net/manual/en/function.hash-hmac.php
http://us.php.net/base64_encode
In Javascript, I'm using JQuery's crypto to do the HMAC piece:
http://code.google.com/p/crypto-js/#HMAC-SHA256
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);
Is this correct? How would I create a javascript / jquery equivalent of the above PHP function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HMAC 是一个标准。 SHA-256 也是如此。因此,无论哪种实现,它们的输出都必须相同。
只能是 Base64 编码存在差异。通常,非字母数字字符是
+
和/
,但您不能指望它们。我已经检查过,两种实现都使用相同的非字母数字字符。但是,您仍然应该“手动”检查几千个字符串。 PHP 中的实现经过了良好的测试。但我不知道 jQuery 中的实现是否也是如此......
Base64 编码输出的语法是:
HMAC is a standard. So is SHA-256. So their outputs, regardless of which implementation, has to be the same.
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.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...
The syntax for Base64 encoded output is:
如果您需要 PHP 函数的 JS 实现的灵感,请查看 PHPJS.org。可以在以下位置找到
base64_encode
的 JavaScript 等效项:base64_encode。If you ever need inspiration for a JS implementation of a PHP function, have a look at PHPJS.org. The JavaScript equivalent for
base64_encode
can be found at: base64_encode.