最有效的方法来更改密码的哈希类型(md5到sha1)

Fel*_*sco 3 php database hash md5 sha1

我有一个系统使用MD5来散列用户的密码并将其存储到我的数据库中.现在,我正在改用另一个使用SHA1(以及一个独特的系统SALT,而不是用户唯一的)来散列密码的系统.

如何通过PHP将用户旧的MD5密码设置为我的新SHA1密码?

Bab*_*aba 5

您无法转换md5sha但实际上您的用户只能使用密码,about to login因此您可以稍微修改脚本以自动执行更新

// The user is not authticated yet
$auth = false;
$updated = false;

// From your Login form
$user = $_POST['user'];
$pass = $_POST['pass'];

// Check If the username has update password
$udated = false; // not update

// I gues you always do this
$password = $updated ? md5($pass) : sha1($pass);

// Do the autentication
// Slect from Database
// Check the data
// Set auth
$auth = true;

// Then chage the password
if ($auth == true && !$updated) {
    $newpassword = sha1($pass);
    // Connect to DB
    // Update the Password
    // Set Status to Updated in DB
    $udated = true;
}

// Better Approch
if ($auth == true && !$updated) {
    $newpassword = password_hash($password, PASSWORD_BCRYPT);
    // Connect to DB
    // Update the Password
    // Set Status to Updated in DB
    $updated = true;
}
Run Code Online (Sandbox Code Playgroud)

我使用password_hash了更好的方法,因为它使用的BCRYPT是更好的哈希算法. 查看有关password_compat的更多信息