Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C#sha256哈希转换为PHP等效格式_C#_Php - Fatal编程技术网

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

将C#sha256哈希转换为PHP等效格式,c#,php,C#,Php,我需要将一些C#代码转换成PHP等价物,以使用SOAP web API。他们所有的例子都用C#表示。我认为我有等效的PHP函数,但我的SOAP请求返回“坏请求”或“未经授权-无效API密钥”-而API页面上的示例页面使用我的密钥,请求URL与传递的摘要消息看起来相同。API和客户端ID绝对正确 以下是C#代码: 下面是我编写的PHP函数,试图实现C#的功能: public static function generateDigest($api_key) { return hash('sha2

我需要将一些C#代码转换成PHP等价物,以使用SOAP web API。他们所有的例子都用C#表示。我认为我有等效的PHP函数,但我的SOAP请求返回“坏请求”或“未经授权-无效API密钥”-而API页面上的示例页面使用我的密钥,请求URL与传递的摘要消息看起来相同。API和客户端ID绝对正确

以下是C#代码:

下面是我编写的PHP函数,试图实现C#的功能:

public static function generateDigest($api_key) {
  return hash('sha256', time() . mb_convert_encoding($api_key, 'UTF-8'));
}
我对C#不是很流利,所以我认为我的错误在于它在做hex.AppendFormat()。我不确定这在PHP中应该是什么。最终结果是一个附加到URL的哈希,以生成SOAP请求,如下所示:

https://payments.homeaway.com/tokens?time=1387385872013&digest=1bd70217d02ecc1398a1c90b2be733ff686b13489d9d5b1229461c8aab6e6844&clientId=[修订]

编辑:

下面是在C#中传递的currentTime变量


我在将其转换为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);
}
函数generateDigest($time,$api_key){
$hash=hash('sha256',$time.mb_convert_编码($api_键,'UTF-8'),true);
返回$this->hexToStr($hash);
}
函数hexToStr($string){
//返回bin2hex($string);
$hex=“”;
对于($i=0;$i如果(ord($string[$i])如果有人在寻找这个答案,那么答案与时间有关。PHP的
time()
函数以秒为单位返回时间,而以C#为单位的调用返回毫秒

因此,获取
$currentTime
的正确方法是

$currentTime = time() * 1000;

这已经用API进行了测试。

为什么
currentTime
a
long
?它是一个时间戳,例如
time()
生成?更新带有currentTime答案的帖子。
 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);
}
$currentTime = time() * 1000;