dongnai8013 2017-06-23 11:39
浏览 667
已采纳

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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条