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生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作