Javascript:等效于带有 RAW BINARY 输出的 PHP 的 hash_hmac()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12099092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Equivalent of PHP's hash_hmac() with RAW BINARY output?
提问by Kevin Cooper
I am connecting to the Amazon Product Advertising API, and to sign my request I need to base64-encode the raw binaryoutput of an HMAC-SHA256 hash.
我正在连接到亚马逊产品广告 API,为了签署我的请求,我需要对HMAC-SHA256 哈希的原始二进制输出进行 base64 编码。
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.
在hash_hmac 的 PHP 文档中,第四个参数bool $raw_output
控制输出是原始二进制数据 (true) 还是小写十六进制数据 (false)。我的程序只需将该参数设置为 true 即可在 PHP 中运行。
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?
但是,我现在正在尝试将其移植到 Javascript。我尝试使用 CryptoJS.HmacSHA256() 函数,但它似乎返回了小写的十六进制。如何将其转换为二进制?
I have tried the following according to the CryptoJS documentation, but both outputs are identical:
我根据 CryptoJS 文档尝试了以下操作,但两个输出是相同的:
var hash = CryptoJS.HmacSHA256("hello", "key");
console.log(hash.toString());
console.log(hash.toString(CryptoJS.enc.Base64));
回答by Esailija
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.jsfor this. If you didn't include this, CryptoJS.enc.Base64
will be undefined
and fallback to the default.
为此,您需要包含http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js。如果你没有包括这个,CryptoJS.enc.Base64
将是undefined
并回退到默认值。
Working demo: http://jsfiddle.net/ak5Qm/
工作演示:http: //jsfiddle.net/ak5Qm/
回答by muhawo
php code
代码
echo base64_encode(hash_hmac('SHA1', 'shanghai', '0', true).'beijing');
php output
php输出
xvBv49PpaYvXAIfy3iOSDWNQj89iZWlqaW5n
node code
节点代码
var crypto = require('crypto');
var buf1 = crypto.createHmac("sha1", "0").update("shanghai").digest();
var buf2 = Buffer.from('beijing');
console.log(Buffer.concat([buf1, buf2]).toString('base64'));
node output
节点输出
xvBv49PpaYvXAIfy3iOSDWNQj89iZWlqaW5n
回答by vincent baronnet
PHP:
PHP:
base64_encode(hash_hmac('sha256', $value, $key, true));
Nodejs equivalent:
Nodejs 等效项:
const crypto = require('crypto');
let token = crypto.createHmac("sha256", key).update(value).digest().toString('base64');
回答by Mohit23x
You can also use this npm package to do the same in Javascript.
你也可以使用这个 npm 包在 Javascript 中做同样的事情。
var jsSHA = require('jssha');
hmac_sha1(string, key){
let shaObj = new jsSHA("SHA-1", "TEXT");
shaObj.setHMACKey(key, "TEXT");
shaObj.update(string);
let hmac = shaObj.getHMAC("B64");
return hmac;
};
回答by Yoann Buzenet
This worked for me :
这对我有用:
var CryptoJS = require("crypto-js");
const raw_signature = hmacSHA1(baseString, signingKey);
const signature = raw_signature.toString(CryptoJS.enc.Base64);
It's giving the exact same result as, in PHP, :
它给出的结果与在 PHP 中完全相同:
$rawSignature = hash_hmac("sha1", $baseString, $signingKey, true);
$signature = base64_encode($rawSignature);