dongnai8013
2017-06-23 11:39
浏览 373

HMAC_SHA1在PHP和Lua上有所不同

I'm trying to generate a HMAC_SHA1 in php, and validate it in openresty lua

PHP Code:

$hmac_sha1 = hash_hmac('sha1', 'test', 'gabri', true);
echo base64_encode($hmac_sha1);

Which produces:

/ReAJgDe67/lF3BNbaGSCx70J/c=

And the same code in lua:

local hmac_sha1 = ngx.hmac_sha1("test", "gabri")
ngx.log(ngx.NOTICE,  ngx.encode_base64(hmac_sha1) );

produces:

Yczcenrc2EAOpfm9UEWwME9XLRI=

Why are they different?

In PHP, I've included the 4th parameter on hash_hmac, which return the data as raw binary

As per: 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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在php中生成HMAC_SHA1,并在openresty lua中验证它 \ n

PHP代码:

  $ hmac_sha1 = hash_hmac('sha1','test','gabri',true); 
echo base64_encode($ hmac_sha1); \  n   
 
 

产生:

  / ReAJgDe67 / lF3BNbaGSCx70J / c = 
    
 
 

和lua中相同的代码:

  local hmac_sha1 = ngx.hmac_sha1(“test”,“gabri”)
ngx.log(ngx。 注意,ngx.encode_base64(hmac_sha1)); 
   
 
 

生成:

  Yczcenrc2EAOpfm9UEWwME9XLRI = 
   
 
 

为什么它们不同?

在PHP中,我在hash_hmac中包含了第4个参数,它将数据作为原始二进制文件返回

按照: https:// github。 com / openresty / lua-nginx-module #ngxhmac_sha1

将生成HMAC-SHA1摘要的原始二进制形式,使用 例如,ngx.encode_base64,如果需要,将结果编码为文本 表示。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanrang3357 2017-06-23 12:07
    已采纳

    According docs

    string hash_hmac(string $algo, string $data, string $key [, bool $raw_output = false]) digest = ngx.hmac_sha1(secret_key, str)

    So in hash_hmac('sha1', 'test', 'gabri', true); test is data and gabri is key. But in ngx.hmac_sha1("test", "gabri") gabri is data and test is key

    点赞 打赏 评论

相关推荐 更多相似问题