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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵