有没有办法将MD5
密码转换为可以验证的东西password_verify()
?
我在Crypt Wikipedia 页面上读到“密码哈希的可打印形式以.MD5
开头”。$1$
因此,我试一试(没有任何运气):
$password = "abcd1234";
$md5hash = "$1$".md5($password);
var_dump(password_verify($password, $md5hash));
有没有办法password_verify()
使用MD5
密码?
问题原因:我有一个旧系统,其中密码存储为MD5
哈希值。我想开始使用更安全的Password Hashing API
. 如果我能够将现有的密码散列转换为可以使用的东西password_verify()
,我可以只更新数据库条目($1$
添加到所有密码散列之前),我的程序将使用以下代码很好地工作(我不必制作旧MD5
密码的特殊情况):
$password; // Provided by user when trying to log in
$hash; // Loaded from database based on username provided by user
if(password_verify($password, $hash)) {
// The following lines will both update the MD5 passwords
// and all passwords whenever the default hashing algorithm is updated
if(password_needs_rehash($hash, PASSWORD_DEFAULT)) {
$hash = password_hash($password, PASSWORD_DEFAULT);
// Store the new hash in database
}
// User is logged in
} else {
// User is not logged in
}