在登录时从数据库中调用带盐的河豚加密

recalling blowfish encryption w/ salt from database at login

我显然是河豚加密的新手,所以才问这个问题。我相信已经弄清楚了等式的一侧,但是一旦哈希在数据库中就无法弄清楚如何登录。我有以下用于加密注册密码的方法:

    $blowfish_hash = "y$";
    $salt_length = 22;

    $salt = Generate_Salt($salt_length);
    $hash_combined = $blowfish_hash . $salt;

    $hash = crypt($password, $hash_combined);

    $password = $hash;

Generate_Salt()函数如下:

function Generate_Salt($length) {

  $unique_rndm_str = md5(uniqid(mt_rand(), true));
  $base64_string = base64_encode($unique_rndm_str);

  $mod_Base64_str = str_replace('+', '.', $base64_string); 
  $salt = substr($mod_Base64_str, 0, $length);

    return $salt;
}

一旦我注册,我就得到了这个漂亮的长散列 - 太棒了!但是,当我登录时,我不确定如何调用散列来检查给定的密码:$_POST['log_password'];

使用 md5 很简单,我只是用这种方式加密 $password = md5($password); 并用这种方式回忆起来 $password = md5($_POST['log_password']); 然而,仔细阅读后我意识到这不是一种安全的方法。

我已经处理这个问题好几个小时了,有人能帮我解释一下吗?任何帮助,将不胜感激。

ep

这比你想象的要容易得多。只需使用函数 password_hash(),它将调用 crypt() 函数并处理安全盐的生成。

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($_POST['password'], PASSWORD_DEFAULT);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($_POST['password'], $existingHashFromDb);