首页 文章

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

提问于
浏览
3

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

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

5 回答

  • 1

    您无法将 md5 转换为 sha ,但实际上您的用户只有在 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;
    }
    

    我使用 password_hash 有一个更好的方法,因为它使用 BCRYPT 这是一个更好的哈希算法 . See more information on password_compat

  • 5

    对不起,你不能 .

    您可以期望的最好的方法是存储MD5和SHA1版本,并在用户登录时填充SHA1内容 . 只需检查SHA1版本是否可用,以及是否使用旧的验证策略 .

    最终,您应该透明地将大多数用户迁移到基于SHA1 / SALT的新系统 .

  • 1

    如果用户不重新输入密码,则无法更改哈希类型 . 它们是不可逆转的单向哈希 . 我猜,你可以尝试在彩虹表中进行查找,但由于某些哈希值存在多次冲突,因此在100%的时间内也不会有效 . 而且,你的盐会使效果无效 . 这就是吃盐的重点 .

  • 1

    您需要原始明文密码才能创建它们的SHA1版本 . 但是,MD5哈希当然是一种方式 . 因此,除非你碰巧有明文版的密码,否则无法做你想做的事 .

  • 4

    您可以在密码表中构建第二个SHA1字段,当用户登录时,它可以检查md5哈希(如果还没有sha1哈希),如果它是正确的,则将其重新哈希到sha1并存储它 . 一旦所有用户都切换到SHA1,您就可以删除md5字段 . - 你给MD5哈希盐腌了吗?

相关问题