3 回答

TA贡献1848条经验 获得超10个赞
我也在寻找这个,尽管它只是一个小的varibale的错误,在hash_hmac函数中将最后一个值传递给true,这将返回原始输出,当您将其转换为base64时,它将给出与c#代码相同的输出。
$buffer = $userName.$accessKey.$timeStamp.$originUrl;
$hmac = hash_hmac('sha256', $buffer, $tokenSecret, true);
$authenticationKey = base64_encode($hmac);
只需在 c# 函数中使用键,如下面的代码所示
var hmac = new System.Security.Cryptography.HMACSHA256();
hmac.Key = System.Text.Encoding.UTF8.GetBytes(tokenSecret);
在我的代码中,我有bas64形式的tokenSecret变量。

TA贡献1847条经验 获得超7个赞
在 c# 代码中,使用了随机生成的密钥。查看文档:https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hmacsha256?view=netframework-4.8#constructors
HMACSHA256()
使用随机生成的密钥初始化 HMACSHA256 类的新实例。
HMACSHA256(字节[])
使用指定的关键数据初始化 HMACSHA256 类的新实例
现在比较php文档:https://www.php.net/manual/en/function.hash-hmac.php
hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : string
你看,你在php中调用buffer的东西实际上是键,你在c#中调用buffer的是你散列的数据。
因此,应包含要散列的数据。$string
根据转换,您不需要在php中获取字节:这个C#编码代码的PHP等效物是什么?
- 3 回答
- 0 关注
- 127 浏览
添加回答
举报