我正在尝试将我的聊天应用程序登录与我的MediaWiki登录结合起来。聊天应用程序有一种奇怪的身份验证方式,我已修改它以使用数据库。
我正在尝试将用户在聊天登录中输入的密码与存储在MediaWiki用户表中的密码相匹配,但我无法弄清楚MediaWiki如何散列其密码。我知道我正在使用默认的盐渍哈希。有没有人有能够重现这个的功能?
我试过了:
hash('md5', $password);
但还有更多我无法弄清楚的。
答案 0 :(得分: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
是要提供验证的密码。
答案 1 :(得分: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 docs的问题评论杰克的评论。