我正在尝试在 php 中生成一个 HMAC_SHA1,并在 openresty lua 中验证它
PHP代码:
$hmac_sha1 = hash_hmac('sha1', 'test', 'gabri', true);
echo base64_encode($hmac_sha1);
其中产生:
/ReAJgDe67/lF3BNbaGSCx70J/c=
和 lua 中的相同代码:
local hmac_sha1 = ngx.hmac_sha1("test", "gabri")
ngx.log(ngx.NOTICE, ngx.encode_base64(hmac_sha1) );
产生:
Yczcenrc2EAOpfm9UEWwME9XLRI=
他们为什么不同?
在 PHP 中,我在 hash_hmac 上包含了第四个参数,它将数据作为原始二进制文件返回
根据:https : //github.com/openresty/lua-nginx-module#ngxhmac_sha1
将生成 HMAC-SHA1 摘要的原始二进制形式,例如,使用 ngx.encode_base64 将结果编码为文本表示(如果需要)。
根据文档
string hash_hmac(string $algo, string $data, string $key [, bool $raw_output = false])
digest = ngx.hmac_sha1(secret_key, str)
所以hash_hmac('sha1', 'test', 'gabri', true);
test
数据gabri
是关键。但 inngx.hmac_sha1("test", "gabri")
gabri
是数据并且test
是关键
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句