简体   繁体   English

将C#sha256哈希转换为等效的PHP

[英]Convert C# sha256 hashing to PHP equivalent

I need to convert some C# code into a PHP equivalent to work with a SOAP web API. 我需要将一些C#代码转换为等效于SOAP Web API的PHP。 All of their examples are in C#. 他们所有的示例都在C#中。 I think I have the equivalent PHP function, but my SOAP requests are returning either 'Bad Request' or 'Unauthorized - Invalid API Key' - while the example page on the API page works with my keys and the request URLs look the same sans the digest message being passed. 我想我具有等效的PHP函数,但是我的SOAP请求返回的是“错误请求”或“未授权-无效的API密钥”,而API页面上的示例页面可以使用我的密钥,并且请求URL看起来相同摘要消息正在传递。 The API and client ID are definitely correct. API和客户端ID绝对正确。

Here is the C# code: 这是C#代码:

private string GenerateDigest(long currentTime)
    {
        SHA256Managed hashString = new SHA256Managed();
        StringBuilder hex = new StringBuilder();
        byte[] hashValue = hashString.ComputeHash(Encoding.UTF8.GetBytes(String.Format("{0}{1}", currentTime, txtApiKey.Text)));

        foreach (byte x in hashValue)
        {
            hex.AppendFormat("{0:x2}", x);
        }

        return hex.ToString();
    }

Here is the PHP function I wrote to try to do what C# is doing: 这是我编写的PHP函数,用于尝试执行C#的工作:

public static function generateDigest($api_key) {
  return hash('sha256', time() . mb_convert_encoding($api_key, 'UTF-8'));
}

I am not very fluent in C# so I assume where I am going wrong is where it is doing hex.AppendFormat(). 我不太精通C#,所以我认为我做错的地方是在做hex.AppendFormat()。 I am not sure what this is supposed to be in PHP. 我不确定这在PHP中应该是什么。 The end result is a hash that is appended to a URL to generate a SOAP request, like this: 最终结果是将哈希附加到URL上以生成SOAP请求,如下所示:

https://payments.homeaway.com/tokens?time=1387385872013&digest=1bd70217d02ecc1398a1c90b2be733ff686b13489d9d5b1229461c8aab6e6844&clientId=[redacted] https://payments.homeaway.com/tokens?time=1387385872013&digest=1bd70217d02ecc1398a1c90b2be733ff686b13489d9d5b1229461c8aab6e6844&clientId= [已编辑]

Edit: 编辑:

Here is the currentTime variable being passed in C#. 这是在C#中传递的currentTime变量。

// Request validation setup
TimeSpan timeSinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
long currentTime = (long)timeSinceEpoch.TotalMilliseconds;
string digest = GenerateDigest(currentTime);

I've the same problem coverting this to php code here's my code to solve the problem : 我有同样的问题覆盖到php代码,这是我解决问题的代码:

 function generateDigest($time, $api_key) {
    $hash = hash('sha256', $time . mb_convert_encoding($api_key, 'UTF-8'), true);
    return $this->hexToStr($hash);
}

function hexToStr($string){
    //return bin2hex($string);
    $hex="";
    for ($i=0; $i < strlen($string); $i++)
    {
        if (ord($string[$i])<16)
            $hex .= "0";
        $hex .= dechex(ord($string[$i]));
    }
    return ($hex);
}

If anyone is looking for this the answer has to do with time. 如果有人在寻找答案,答案与时间有关。 PHP's time() function returns time in seconds where the call in C# returns milliseconds. PHP的time()函数以秒为单位返回时间,其中C#中的调用返回毫秒。

Therefore, the correct way to get $currentTime is 因此,获取$currentTime的正确方法是

$currentTime = time() * 1000;

This has been tested with the API. 已通过API测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM