作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是 C# 和 PHP 的代码,我需要一些帮助。我正在尝试生成 C# 中的authenticationKey,但希望在 PHP 中进行转换。一切都完成了,但我不知道如何在 PHP 中实现 [System.Text],因为 PHP 中有 hash_hmac() 但 $ 可能是什么同一函数中的字符串。
C#版本
var hmac = new System.Security.Cryptography.HMACSHA256();
var buffer = userName + accessKey + timeStamp + originUrl;
var hash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(buffer));
var authenticationKey = Convert.ToBase64String(hash);
PHP版本
$hmac = hash_hmac('sha256', $string, $buffer);
$encoded = base64_encode($hmac);
任何人都可以帮我解决这个问题吗?这将会非常有帮助。谢谢。
最佳答案
我也在寻找这个,最后这只是 1 个小变量的错误,在 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 变量。
关于c# - 如何在PHP中获取C#的System.Text?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014834/
我是一名优秀的程序员,十分优秀!