Created
May 10, 2016 20:55
-
-
Save ianchanning/b9e0c43c80c2ffb05220da2ceeca9e60 to your computer and use it in GitHub Desktop.
Hash and verify passwords in PHP - The right way. Based off https://www.reddit.com/r/PHP/comments/3lwxlw/hash_and_verify_passwords_in_php_the_right_way/cva6y6p
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function login($username, $password) { | |
$options = array('cost' => 10); | |
$userdata = 'query to find the user in your framework/PDO prepared query'; | |
$plainText = $password; | |
if ($userdata['legacy']) { | |
$password = md5($password); | |
} | |
if (!password_verify($password, $userdata['password'])) { | |
return false; // or a not-authorised error or whatever. | |
} | |
if ($user['legacy'] || password_needs_rehash($userdata['password'], PASSWORD_DEFAULT, $options)) { | |
$hash = password_hash($plainText, PASSWORD_DEFAULT, $options); | |
// store $hash in the password field | |
// set the legacy value to 0 | |
} | |
return true; //or an authorised flag, etc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment