令人痛苦的SyncML同步協議錯誤

來源:互聯網
上載者:User

今天上午一直在翻OMA的SyncML 1.1 協議,卡在使用者驗證的地方,按照協議上的例子怎麼算也算不出來預期的結果。

根據協議本文,MD5驗證的演算法很簡單,用虛擬碼來表示就是這樣:

result = base64( md5( base64( md5( username + ":" + password)) + ":" + nonce) );

用php寫出來就是:

function computehash($username, $password, $nonce){
    return base64_encode(pack('H*', md5(base64_encode(pack('H*', md5("$username:$password"))) . ":$nonce")));
}

但是用協議裡指定的參數:

username="Bruce1" , password="OhBehave" , nonce="Nonce"

無論如何也算不出協議裡給的值"Zz6EivR3yeaaENcRN6lpAQ==", 而是 "+MEIKFrF8Qdou2B1H2YmXw=="

一氣之下用 .NET 寫了一個,結果也是相同的。

Code
/// <summary>
/// Generate MD5 digest for SyncML authentication
/// </summary>
/// <param name="user"></param>
/// <param name="password"></param>
/// <param name="nonce"></param>
/// <returns></returns>
public static string GenerateSyncMLMD5(string user, string password, string nonce)
{
    System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();

    md5.ComputeHash(Encoding.ASCII.GetBytes(user + ":" + password));

    string s = Convert.ToBase64String(md5.Hash) + ":" + nonce;

    md5.ComputeHash(Encoding.ASCII.GetBytes(s));
    return Convert.ToBase64String(md5.Hash);
}

經過多方搜尋,終於在 這裡 發現了原因:協議寫錯了。正確的參數應該是:

username="Bruce2" , password="OhBehave" , nonce="Nonce"

我Faint…

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.