duanjie5570 2018-09-03 10:45
浏览 874

如何在节点js中使用hash_hmac函数

Recently I have used same code on PHP and it's working fine but when I tried Node Js, it's not working for me. Please check once:

PHP

$signature = $ACCID . "POST" . strtolower(urlencode($url)).$requestContentBase64String; 
$hmacsignature = base64_encode(hash_hmac("sha256", $signature, base64_decode($APIKey), true)); 

NODE CODE :

var signature = ACCID+"POST"+encodeURI(url).toLowerCase()+requestContentBase64String; 
var hmacsignature = base64.encode(crypto.createHmac('sha256', APIKey).update(signature).digest('base64'))

Please check what's wrong in this code.

  • 写回答

1条回答 默认 最新

  • doupa2871 2018-09-03 11:05
    关注

    Your second line has two mistakes:

    1. PHP APIKey is BASE64_DECODE(APIKey) while in Node.js code it is just APIKey
    2. PHP hmacsignature is BASE64(HEX(HMAC)) while in Node.js code it is BASE64(BASE64(HMAC))

    Try this:

    var hmacsignature = crypto.createHmac('sha256', Buffer.from(APIKey, 'base64')).update(signature).digest('base64')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料