password_verify 和 doveadm pw -t 如何在不加盐的情况下验证密码

How can password_verify and doveadm pw -t verify a password without salt

我目前正在尝试理解哈希和盐。据我了解,如果我只有密码和生成的哈希值(这是用随机盐生成的),就不可能验证密码。 那么,如果我不给它加盐,PHP 中的 password_verify 函数如何验证我的 密码 ?后台是否有一个隐藏变量,为 php 哈希函数存储它?

如果是这样的话,怎么可能

doveadm pw -t '{SHA512-CRYPT}$myhash...' -p "qwertz"

也验证它,即使我 运行 它在完全不同的计算机上?那是 Dovecot(一种 MDA)附带的工具。

这是我的 PHP 代码,它创建一个包含 64 个字符的随机盐,将其与密码组合,创建哈希并通过 password_verify().

验证哈希

我今天才开始处理整个 hash/salt/pepper 事情,所以我的整个思路可能存在巨大缺陷。

<?php
$password = "qwertz";
$salt = createSalt(64);
$hash = crypt($password, "$$salt");

if (password_verify($password, $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}


function createSalt($length){
    $chars = "IrhsYyLofUKj4caz0FDBCe2W9NRunTgQvp7qOXmS5GM3EJV6i8tAHdkPbxwl1Z";
    $salt="";
    for($i=0; $i < $length; $i++){
        $newchar = substr($chars, rand(0,strlen($chars)-1),1);
        $salt .= $newchar;
    }
    return $salt;
}
?>

哈希包含几条信息。 article 解释了 Unix 使用的格式,但我相信 PHP 密码函数使用类似的格式(如果不相同的话):

The hash field itself is comprised of three different fields. They are separated by '$' and represent:

  • Some characters which represents the cryptographic hashing mechanism used to generate the actual hash
  • A randomly generated salt to safeguard against rainbow table attacks
  • The hash which results from joining the users password with the stored salt and running it through the hashing mechanism specified in the first field

它还可以包括用于生成哈希的每个算法的确切选项,例如算法成本:

var_dump(password_hash('foo', PASSWORD_BCRYPT, [
    'cost' => 8,
]));
string(60) "yZ5bTz7xXnom8QsrbZ7uQetMLxOZ7WjuDkUYRIh73Ffa17GV1Tb7q"

这里y$表示使用了成本为8的Bcrypt

如果我们使用 PHP/7.2 中可用的较新的 Argon2,则参数会更多:

$argon2i$v=19$m=1024,t=2,p=2$YzJBSzV4TUhkMzc3d3laeg$zqU/1IN0/AogfP4cmSJI1vc8lpXRW9/S0sYY2i2jHT0

@Álvaro González 回答的一些背景:

PHP 手册建议使用 "password_hash" 而不是 "crypt" 函数通过 "password_hash" 是一个 "crypt()" 包装器(因为,它使用强哈希,生成强盐,并自动应用适当的回合。)

"password_hash()" returns 作为返回哈希的一部分的算法、成本和盐。因此,验证散列所需的所有信息都包含在其中。这允许 "password_verify" 函数验证散列,而无需单独存储盐或算法信息。 : http://php.net/manual/en/function.password-verify.php

因为 "password_hash" 是 "crypt" 的包装器,"crypt" 也做同样的事情,即 returns 算法、成本和盐作为返回的散列。因此 "password_verify" 可以验证散列。

现在,请检查@Álvaro González 给出的答案