如何使用 C# 从 php 哈希中获得相同的结果



我一直在努力,试图获得相当于 PHP API 的 C# 等效物,我收到错误消息作为无效哈希,所以我决定将代码分成几部分并检查 PHP 和 C# 的单个部分的输出。 以下是我得到的:

php 引用的代码和输出:

$ref = time().mt_rand(0,9999999);
----Out put as at the time it was tested----
14909496966594256

在我的 C# 代码中,引用如下所示:

string refl = (DateTime.UtcNow .Subtract( new DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds + rnd.Next(0, 9999999).ToString();
----Out put as at the time it was tested----
1490602845.686821282389

使用以下变量输出的 php 哈希输出如下:

$task = 'pay';
$merchant_email_on_voguepay = 'merchant@example.com';
$ref = '14909496966594256';
$command_api_token = '9ufkS6FJffGplu9t7uq6XPPVQXBpHbaN';
$hash = hash('sha512',$command_api_token.$task.$merchant_email_on_voguepay.$ref);
----Out put ----
1cee97da4c0b742b6d5cdc463914fe07c04c6aff8d999fb7ce7aaa05076ea17577752ecf8947e5b98e7305ef09e0de2fed73e4817d906d6b123e54c1f9b15e74

然后 C# 输出使用相同的变量和相同的 PHP 引用

输出输出
const string task = "Pay";
const string command_api_token = "9ufkS6FJffGplu9t7uq6XPPVQXBpHbaN";  
const string merchant_email_on_voguepay = "merchant@example.com"; 
Random rnd = new Random();
string refl = "14909496966594256";
string hash_target = (command_api_token + task + merchant_email_on_voguepay + refl);
SHA512 sha512 = new System.Security.Cryptography.SHA512Managed();
var bytes = UTF8Encoding.UTF8.GetBytes(hash_target);
string cryString = BitConverter.ToString(sha512.ComputeHash(bytes));
string hashD = (cryString).Replace("-", string.Empty).ToLower();
----Out put ----
551b057b64f49fc6bd7d428a8e3c36ddaab5e468fd5f9042ad5d4a4fa50349e5312ad2097b4e46d1e74a5a3f4e843848352edb0ea7073dd1cd53b1c4c14ab286

在这里,我发现我的 C# 输出与 php 的输出不同。 所以我的代码可能有什么问题,我希望使用相同的变量获得与 php 相同的输出。

欢迎解决此问题的任何好主意。

在 C# 代码中,task的值为"Pay"。 在PHP代码中,它是"pay".

不同的输入值自然不会散列相同。

最新更新