使用Javascript:PHP的hash_hmac()与原始二进制输出的等效? [英] Javascript: Equivalent of PHP's hash_hmac() with RAW BINARY output?

查看:2910
本文介绍了使用Javascript:PHP的hash_hmac()与原始二进制输出的等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连接到亚马逊产品广告API,并签署了我的要求,我需要的base64 EN code在原始二进制的HMAC-SHA256哈希值的输出。

为hash_hmac PHP文档,第四个参数布尔$ raw_output 控制输出是原始二进制数据(真)或小写hexits(假)。我的程序通过简单的参数设置为true在PHP中工作。

不过,我现在想这个端口上为JavaScript。我尝试使用CryptoJS.HmacSHA256()函数,但它似乎是返回小写hexits。我怎样才能将其转换为二进制?

我曾尝试以下根据CryptoJS文档,但是两个输出是相同的:

  VAR哈希= CryptoJS.HmacSHA256(你好,重点);
的console.log(hash.toString());
的console.log(hash.toString(CryptoJS.enc.Base64));


解决方案

这是在他们的文档。试试这个:

  VAR哈希= CryptoJS.HmacSHA256(信息,秘密口令);VAR的base64 = hash.toString(CryptoJS.enc.Base64);

您需要包括<一个href=\"http://crypto-js.google$c$c.com/svn/tags/3.0.2/build/components/enc-base64-min.js\">http://crypto-js.google$c$c.com/svn/tags/3.0.2/build/components/enc-base64-min.js为了这。如果不包括这一点, CryptoJS.enc.Base64 未定义并回退到默认的。

工作演示: http://jsfiddle.net/ak5Qm/

I am connecting to the Amazon Product Advertising API, and to sign my request I need to base64-encode the raw binary output of an HMAC-SHA256 hash.

In the PHP documentation for hash_hmac, the fourth parameter bool $raw_output controls whether the output is raw binary data (true) or lowercase hexits (false). My program works in PHP by simply setting that parameter to true.

However, I am now trying to port this over to Javascript. I tried using the CryptoJS.HmacSHA256() function, but it seems to be returning the lowercase hexits. How can I convert this to binary?

I have tried the following according to the CryptoJS documentation, but both outputs are identical:

var hash = CryptoJS.HmacSHA256("hello", "key");
console.log(hash.toString());
console.log(hash.toString(CryptoJS.enc.Base64));

解决方案

This is explained in their documentation. Try this:

var hash = CryptoJS.HmacSHA256("Message", "Secret Passphrase");

var base64 = hash.toString(CryptoJS.enc.Base64);

You need to include http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js for this. If you didn't include this, CryptoJS.enc.Base64 will be undefined and fallback to the default.

Working demo: http://jsfiddle.net/ak5Qm/

这篇关于使用Javascript:PHP的hash_hmac()与原始二进制输出的等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆