HMAC_SHA1 PHP 和 Lua 不同

HMAC_SHA1 differ on PHP and Lua

我正在尝试在 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 上包含了第 4 个参数,return 数据作为原始二进制文件

根据: https://github.com/openresty/lua-nginx-module#ngxhmac_sha1

The raw binary form of the HMAC-SHA1 digest will be generated, use ngx.encode_base64, for example, to encode the result to a textual representation if desired.

根据文档

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是关键。 但是在 ngx.hmac_sha1("test", "gabri") gabri 是数据而 test 是关键