如何在PHP中复制此C#哈希? (toByteArray(),ComputeHash()) [英] How can I replicate this C# hashing in PHP? (toByteArray(), ComputeHash())

查看:313
本文介绍了如何在PHP中复制此C#哈希? (toByteArray(),ComputeHash())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PHP中复制以下代码,这是我必须与之交互的API的示例代码(该API&示例代码在C#中,我的应用程序在PHP 5.3中).我不是C#开发人员,因此在执行此操作时遇到了麻烦.

I am trying to replicate the following code in PHP, It is example code for an API I have to interface with (The API & Example code is in C#, My app is in PHP 5.3). I'm not a C# developer and so am having trouble doing this.

// 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() 和其他功能进行多种变体我在网上找到了,但是没有什么可比拟的,我不知道我做对了还是不正确.

I've tried many variations using pack() and other functions I've found online, but without anything to compare it to, I don't know if i've done it right or not.

任何C#开发人员都可以运行上面的代码并发布生成的值,以便我可以使用该代码进行检查/测试吗?

Can any C# devs run the above code and post the values generated so I can use that to check/test against?

我尝试检查 MSDN 来查看这些方法的功能,但是卡住了(并且不确定它是否正确,因为我没有什么可比较的).

I've tried checking the MSDN to see what these methods do, but am stuck (and not sure if its correct, as I have nothing to compare it to).

// 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#方法中的几种是什么.

This sounds pretty simple and straightforwaard, but I'm not sure what a few of these C# methods do..

  • ComputeHash(byteArray)-计算得出什么?..返回什么?
  • System.Text.Encoding.ASCII.GetBytes(stringToSign);-这会返回什么?
  • new HMACSHA1(new Guid(apiToken).toByteArray());没有秘密密钥?,使用的密钥是什么?
  • ComputeHash(byteArray) - Computed to what?.. what is returned?
  • System.Text.Encoding.ASCII.GetBytes(stringToSign); - What does this return?
  • new HMACSHA1(new Guid(apiToken).toByteArray()); No Secret Key?, what is the key used?

任何资源或帮助将不胜感激. 我在SO上尝试了其他答案的变体,但没有喜悦.

Any resources or help would be much appreciated. I tried variations of other answers on SO, but no joy.

我可以在线运行3行代码(例如JSFiddle,但对于C#吗?),以便可以看到每一行的输出?

Can I run the 3 lines of code somewhere online (like JSFiddle but for C#?) so I can see the output of each line?


仍然遇到问题,我设法在Visual Studio中测试了C#代码,但是在获取PHP中生成的相同哈希值时遇到了麻烦.

Still having trouble with this, I have managed to test the C# code in Visual Studio, but am having trouble getting the same hash generated in PHP.

..上面的C#代码(特别是创建SHA1哈希的三行代码)将转换为PHP(请查看我在上面发布的Pseudo Code).我应该能够使用PHP匹配C#哈希.

.. the above C# code (specifically, the 3 lines which create the SHA1 hash) to be converted into PHP (Check out the Pseudo Code I posted above). I should be able to match the C# hash using PHP.

如果还有其他问题,请询问.

If you have any other questions, please ask.

推荐答案

问题是GUID的字符串形式颠倒了GUID的前3个段中2个字符的十六进制数字的顺序.有关更多信息,请参见以下示例中的注释: http://msdn.microsoft.com/zh-CN/library/system.guid.tobytearray.aspx

The issue is that the string form of the GUID reverses the order of the 2-character hexadecimal numbers in the first 3 segments of the GUID. For more information see the comments in the example at: http://msdn.microsoft.com/en-us/library/system.guid.tobytearray.aspx

下面的代码应该起作用:

The following code should work:

$apiTokenId = 1887;
$apiToken = "E1024763-1234-5678-91E0-FF2E4E7EB316";
$stringToSign = '';
$hexStr = str_replace('-','',$apiToken);
$c = explode('-',chunk_split($hexStr,2,'-'));
$hexArr = array($c[3],$c[2],$c[1],$c[0],$c[5],$c[4],$c[7],$c[6],$c[8],$c[9],$c[10],$c[11],$c[12],$c[13],$c[14],$c[15]);
$keyStr = '';
for ($i = 0; $i < 16; ++$i) {
    $num = hexdec($hexArr[$i]);
    $keyStr .= chr($num);
}
$stringToSign .= "POST" . "UserAgent" . "http://api.com/post";
$hmacsha1 = base64_encode(hash_hmac('sha1',$stringToSign,$keyStr,true));

我已经针对您上面提供的C#代码测试了此代码,并且输出是相同的.但是,原始代码中指定的GUID无效,因此我必须对其稍作更改.

I've tested this code against the C# code you provided above and the output was the same. However, the GUID specified in the original code is not valid so I had to change it slightly.

这篇关于如何在PHP中复制此C#哈希? (toByteArray(),ComputeHash())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆