我正在尝试复制以下代码PHP
,这是我必须与之交互的 API 的示例代码(API 和示例代码位于C#
,我的应用程序在PHP 5.3
)。我不是 C# 开发人员,因此在执行此操作时遇到困难。
// C# Code I am trying to replicate in PHP
var apiTokenId = 1887;
var apiToken = "E1024763-1234-5678-91E0-T32E4E7EB316";
// Used to authenticate our request by the API (which is in C#)
var stringToSign = string.Empty;
stringToSign += "POST"+"UserAgent"+"http://api.com/post";
// Here is the issue, How can I do the following 3 lines in PHP?
// No "secret key" provided?.. How do I do this in PHP?
var hmacsha1 = new HMACSHA1(new Guid(apiToken).toByteArray());
// Make a byte array with ASCII encoding.
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(stringToSign);
// Finally, 'computeHash' of the above (what does this do exactly?!)
var calculatedSignature = Convert.ToBase64String(hmacsha1.ComputeHash(byteArray));
我尝试过很多变体使用pack() http://php.net/manual/en/function.pack.php以及我在网上找到的其他功能,但没有任何可比较的东西,我不知道我做得对还是错。
任何 C# 开发人员都可以运行上述代码并发布生成的值,以便我可以使用它来检查/测试吗?
我试过检查MSDN http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm.computehash%28v=vs.71%29.aspx看看这些方法的作用,但我被卡住了(不确定它是否正确,因为我没有什么可比较的)。
PHP 伪代码
// Set vars
$apiToken = 'E1024763-1234-5678-91E0-T32E4E7EB316';
$apiTokenId = '1887';
$stringToSign = "POST"."UserAgent"."http://api.com/post";
// HowTo: Build a `byteArray` of our apiToken? (i think)
// C#: var hmacsha1 = new HMACSHA1(new Guid(apiToken).toByteArray());
// HowTo: Convert our $stringToSign to a ASCII encoded `byteArray`?
// C#: byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(stringToSign);
// HowTo: Generate a base64 string of our (`hmacsha1`.ComputeHash(byteArray))
// C#: var calculatedSignature = Convert.ToBase64String(hmacsha1.ComputeHash(byteArray));
这听起来非常简单明了,但我不确定其中一些 C# 方法的作用。
这些有什么作用C#
方法执行/返回?
-
ComputeHash(byteArray)
- 计算什么?...返回什么?
-
System.Text.Encoding.ASCII.GetBytes(stringToSign);
- 这会返回什么?
-
new HMACSHA1(new Guid(apiToken).toByteArray());
没有密钥?,使用什么密钥?
任何资源或帮助将不胜感激。
我尝试过的变体other https://stackoverflow.com/questions/13064942/net-c-sharp-to-php-pb-with-byte-array-computehash回答如此,但没有喜悦。
我可以在网上的某个地方运行这 3 行代码吗(比如JSFiddle
但对于C#
?)这样我就可以看到每行的输出?
更新 - 添加赏金
仍然遇到问题,我已经成功测试了C#
在 Visual Studio 中编写代码,但在获取生成的相同哈希时遇到问题PHP
.
我想...
.. 以上C#
代码(具体来说,创建的 3 行SHA1
hash)转换为 PHP(查看Pseudo Code
我在上面发过)。我应该能够匹配C#
使用 PHP 进行哈希处理。
如果您还有其他问题,请询问。