2012-11-16 116 views
1

我想將我的聊天應用程序登錄和我的MediaWiki登錄結合起來。聊天應用程序有一種奇怪的身份驗證方式,我已將其修改爲使用數據庫。如何根據MediaWiki用戶表驗證密碼散列密碼?

我想將用戶在聊天登錄時輸入的密碼與保存在MediaWiki用戶表中的密碼進行匹配,但我無法弄清楚MediaWiki如何哈希密碼。我知道我正在使用默認的鹽漬散列。有沒有人有一個功能,可以重新創建?

我曾嘗試:

hash('md5', $password); 

,但還有更多的它,我想不通。

+0

你看過媒體wiki的源代碼嗎? – jordanm

+0

是的,但我無法找到加密功能 – arrowill12

+1

http://www.mediawiki.org/wiki/Manual_talk:User_table –

回答

1

這都是直把我的頭頂部,但:

<?php 
//let's pretend these were entered by the user trying to authenticate 
$username = 'ted'; 
$password = 'password'; 

//PDO-like syntax. I wrote my own class around it and I don't remember how to do it raw. 
$query = "SELECT pw_hash FROM user_table WHERE username = ?"; 
$rs = $dbh->doQuery($query, array($username)); 
if(count($rs) == 0) { throw new Exception('no user'); } 

//will be either 
//:A:5f4dcc3b5aa765d61d8327deb882cf99 
//:B:838c83e1:e4ab7024509eef084cdabd03d8b2972c 

$parts = explode(':', $rs[0]['pw_hash']); 
print_r($parts); //$parts[0] will be blank because of leading ':' 

switch($parts[1]) { 
    case 'A': 
     $given_hash = md5($password); 
     $stored_hash = $parts[2]; 
     break; 
    case 'B': 
     $given_hash = md5($parts[2] . md5($password)); 
     $stored_hash = $parts[3]; 
     break; 
    default: 
     throw new Exception('Unknown hash type'); 
     break; 
} 

if($given_hash === $stored_hash) { 
    echo "login was succesful."; 
} else { 
    echo "login failed."; 
} 

道具傑克的問題與this link到MediaWiki文檔註釋。

+0

這是非常有幫助的,我現在試圖實現它。當我查詢密碼時,密碼會作爲「小塊」存儲在數據庫中,它會得到哈希還是一些二進制文件? – arrowill12

+0

打我。在結果集上嘗試'var_dump()'並[試試這個](http://stackoverflow.com/questions/948174/how-do-i-convert-from-blob-to-text-in-mysql)到必要時轉換。 – Sammitch

+0

我只是'回聲'的結果,他們正常感謝您的幫助 – arrowill12

1

如果this wiki page是可以相信,以驗證對存儲數據庫值的密碼,你可以這樣做:

list($dummy, $type, $salt, $hash) = explode(':', $db_password); 

$pwd_hash = md5($user_password); 
if ($type == 'B' && md5("$salt-$pwd_hash") === $hash) { 
    // yay success, type B 
} elseif ($type == 'A' && $salt === $pwd_hash) { 
    // yay success, type A 
} else { 
    // password is wrong or stored password is in an unknown format 
} 

假設$db_password是數據庫的價值和$user_password是提供密碼,以驗證反對。

+0

「哎呀,對不起。」可能是因爲密碼格式無效或密碼錯誤。 – NullUserException

+0

@NullUserException是的,好點。更新。 –