Use bcrypt to hash passwords

Replace the default hash function used for storing passwords by
password_hash() which internally uses bcrypt. Legacy MD5 hashes are
still supported and are immediately converted to the new format when a
user logs in.

Since big parts of the authentication system needed to be rewritten in
this context, this patch also includes some simplification and
refactoring of all code related to password checking and resetting.

Fixes FS#52297.

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
Lukas Fleischer 2017-02-24 19:52:28 +01:00
parent 31754909b1
commit 29a48708bb
5 changed files with 72 additions and 150 deletions

View file

@ -537,63 +537,6 @@ function mkurl($append) {
return substr($out, 5);
}
/**
* Determine a user's salt from the database
*
* @param string $user_id The user ID of the user trying to log in
*
* @return string|void Return the salt for the requested user, otherwise void
*/
function get_salt($user_id) {
$dbh = DB::connect();
$q = "SELECT Salt FROM Users WHERE ID = " . $user_id;
$result = $dbh->query($q);
if ($result) {
$row = $result->fetch(PDO::FETCH_NUM);
return $row[0];
}
return;
}
/**
* Save a user's salted password in the database
*
* @param string $user_id The user ID of the user who is salting their password
* @param string $passwd The password of the user logging in
*/
function save_salt($user_id, $passwd) {
$dbh = DB::connect();
$salt = generate_salt();
$hash = salted_hash($passwd, $salt);
$q = "UPDATE Users SET Salt = " . $dbh->quote($salt) . ", ";
$q.= "Passwd = " . $dbh->quote($hash) . " WHERE ID = " . $user_id;
return $dbh->exec($q);
}
/**
* Generate a string to be used for salting passwords
*
* @return string MD5 hash of concatenated random number and current time
*/
function generate_salt() {
return md5(uniqid(mt_rand(), true));
}
/**
* Combine salt and password to form a hash
*
* @param string $passwd User plaintext password
* @param string $salt MD5 hash to be used as user salt
*
* @return string The MD5 hash of the concatenated salt and user password
*/
function salted_hash($passwd, $salt) {
if (strlen($salt) != 32) {
trigger_error('Salt does not look like an md5 hash', E_USER_WARNING);
}
return md5($salt . $passwd);
}
/**
* Get a package comment
*