WS-Security php中PasswordDigest的工作算法


Working algorithm for PasswordDigest in WS-Security php

我一直在根据航空公司供应商提供的公式创建哈希密码。我在这个网站上搜索了一下,我从下面的C#链接中得到了解决方案,但我想要PHP。WS-Security 中PasswordDigest的工作算法

我在php中尝试过这样的操作,但我得到的密码摘要是错误的

function getTimestamp()
{
$microtime = floatval(substr((string)microtime(), 1, 8));
$rounded = round($microtime, 3);
return gmdate("Y-m-d'TH:i:s") . substr((string)$rounded, 1, strlen($rounded))."Z";
}
$nounce = base64_encode(mt_rand(10000000, 99999999)); 
$timestamp = getTimestamp(); 
$password = "AMADEUS"; //clear password
$final_hashed_password = base64_encode(sha1($nounce.$timestamp.sha1($password)));      

我的价值观正在像这样生成

Nonce: ODczNzczNzE=
Timestamp: 2014-09-21T06:36:31.328Z
password: "TEST"
password digest I got: NjQxOThmZjViNmIwOGM0NGNiNDE1YTExNWQ3MDc2OGNlYjBjZDY2MA==

但是密码摘要应该像这样生成

Right password digest: zGXsP85SuUngY7FjtnQizeO6yUk=

我知道创建摘要的算法是:

Password_Digest = Base64 ( SHA-1 ( nonce + created + SHA-1 ( password ) ) )

请帮助我在php中生成正确的哈希密码,还请参阅上面的链接,该链接在c#

中有解决方案

找到了解决方案!。。。我们必须解码随机数,然后对其应用公式,在xml中,我们必须发送编码的随机数

正如您所提到的,问题发生在Nonce。

如果我可以建议的话,最好使用字节流(random_bytes)作为nonce,而不是不编码的mt_rand(10000000, 99999999)。然后,只在将其包含在nonce SOAP/XML节点中时对其进行编码。

您可以使用此PHP代码生成摘要密码:

<?php
    date_default_timezone_set('UTC');
    $t = microtime(true);
    $micro = sprintf("%03d",($t - floor($t)) * 1000);
    $date = new DateTime( date('Y-m-d H:i:s.'.$micro) );
    echo $timestamp = $date->format("Y-m-d'TH:i:s").$micro . 'Z';
    $nonce = mt_rand(10000000, 99999999);
    echo $nounce = base64_encode($nonce);//we have to decode the nonce and then apply the formula on it and in xml we have to send the encoded nonce
    $password = "AMADEUS"; //clear password
    echo $passSHA = base64_encode(sha1($nonce . $timestamp . sha1($password, true), true));   
 ?>