简体   繁体   English

将哈希HMAC SHA256的端口JS加密代码移植到PHP

[英]Port JS crypto code for hash HMAC SHA256 to PHP

I want to port the following JS code: 我想移植以下JS代码:

crypto.createHmac('sha256', secret)
  .update(s)
  .digest('base64');

To PHP. 到PHP。 How do I do this? 我该怎么做呢?

The closest I got was the following: 我得到的最接近的是:

hash_hmac('sha256', $s, $secret)

or 要么

base64_encode(hash_hmac('sha256', $s, $secret));

But neither returns the same result as the JS version. 但是都没有返回与JS版本相同的结果。

How do I port the JS to PHP 如何将JS移植到PHP

Managed to answer my own question: 设法回答了我自己的问题:

base64_encode(hash_hmac('sha256', $s, $secret, true));

The last parameter is raw_output , which according to the docs : 最后一个参数是raw_output ,根据docs

When set to TRUE, outputs raw binary data. 设置为TRUE时,输出原始二进制数据。 FALSE outputs lowercase hexits. FALSE输出小写的十六进制。

Better solutions are welcome. 欢迎提供更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM