这个C#编码代码的PHP等价物是什么?

这个C#编码代码的PHP等价物是什么?,第1张

概述我想将以下C#代码转换为 PHP. C#是: byte[] operation = UTF8Encoding.UTF8.GetBytes("getfaqs");byte[] secret = UTF8Encoding.UTF8.GetBytes("Password");var hmac = newHMACSHA256(secret);byte[] hash = hmac.ComputeHa 我想将以下C#代码转换为 PHP.

C#是:

byte[] operation = UTF8EnCoding.UTF8.GetBytes("getfaqs");byte[] secret = UTF8EnCoding.UTF8.GetBytes("Password");var hmac = newHMACSHA256(secret);byte[] hash = hmac.ComputeHash(operation);

我已经变成了这个:

$hash = hash_hmac( "sha256",utf8_encode("getfaqs"),utf8_encode("Password"));

然后我有:

var APIKey = "ABC-DEF1234";var authInfo = APIKey + ":" + hash//base64 encode the authorisation infovar authorisationheader = Convert.ToBase64String(EnCoding.UTF8.GetBytes(authInfo));

我认为应该是:

$authInfo = base64_encode($APIKey . ":" . $hash);

要么

$authInfo = base64_encode(utf8_encode($APIKey . ":" . $hash));

但不确定,请注意第二个编码使用EnCoding.UTF8,而不是UTF8EnCoding.UTF8.

PHP代码应该是什么样的?

解决方法 PHP字符串已经(种类)byte [],PHP没有任何编码意识. utf8_encode实际上将ISO-8859-1变为UTF-8,所以这里不需要它.

如果这些字符串是文件中的文字,则该文件只需要以UTF-8编码保存.

将true传递给hash_hmac作为第4个参数并删除那些utf8_encode调用:

$hash = hash_hmac( "sha256","getfaqs","Password",true );

此外,字符串连接运算符是.,所以:

$authInfo = base64_encode($APIKey . ":" . $hash);
总结

以上是内存溢出为你收集整理的这个C#编码代码的PHP等价物是什么?全部内容,希望文章能够帮你解决这个C#编码代码的PHP等价物是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1252508.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存