这个C#编码代码的PHP等价物是什么?
我想把下面的C#代码转换成PHP C#是: 我把它变成了这样:这个C#编码代码的PHP等价物是什么?,c#,php,C#,Php,我想把下面的C#代码转换成PHP C#是: 我把它变成了这样: $hash = hash_hmac( "sha256", utf8_encode("getfaqs"), utf8_encode("Password")); 那么我有: var apiKey = "ABC-DEF1234"; var authInfo = apiKey + ":" + hash //base64 encode the authorisation info var authorisationHeader = Con
$hash = hash_hmac( "sha256", utf8_encode("getfaqs"), utf8_encode("Password"));
那么我有:
var apiKey = "ABC-DEF1234";
var authInfo = apiKey + ":" + hash
//base64 encode the authorisation info
var authorisationHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));
我认为应该是:
$authInfo = base64_encode($apiKey . ":" . $hash);
或
但不确定,请注意第二种编码使用encoding.UTF8,而不是UTF8Encoding.UTF8
PHP代码应该是什么样的?PHP字符串已经(有点)字节[]
,PHP没有任何编码意识utf8_encode
实际上将ISO-8859-1转换为UTF-8,所以这里不需要它
如果这些字符串是文件中的文本,则该文件只需以UTF-8编码保存
将true
传递给hash\u hmac
作为第四个参数,并删除那些utf8\u encode
调用:
$hash = hash_hmac( "sha256", "getfaqs", "Password", true );
此外,字符串连接运算符是
,因此:
$authInfo = base64_encode($apiKey . ":" . $hash);
UTF8编码.UTF8
和Encoding.UTF8
是等效的UTF8Encoding
继承自Encoding
,因此它获得了UTF8
属性。感谢您捕获错误更正。仍然没有进行身份验证,但我认为您的代码很好&我们有额外的problems@jmadsen是的,C#
不完整。在此行之前:var apiKey=“ABC-DEF1234”;var authInfo=apiKey+“:“+hash
,hash
仍然是一个字节[]
,因此如何将其连接到字符串?
$hash = hash_hmac( "sha256", "getfaqs", "Password", true );
$authInfo = base64_encode($apiKey . ":" . $hash);