lavarel的密码验证

lavarel框架的密码验证一般如下:

$hasher = new BcryptHasher;
return $hasher->check($password, $this->password);

Illuminate\Hashing\BcryptHasher中有:

public function check($value, $hashedValue, array $options = [])
    {
        if (strlen($hashedValue) === 0) {
            return false;
        }

        return password_verify($value, $hashedValue);
    }

其中password_verify( value, hashedValue)验证密码是否与哈希匹配。

 public function make($value, array $options = [])
    {
        $hash = password_hash($value, PASSWORD_BCRYPT, [
            'cost' => $this->cost($options),
        ]);

        if ($hash === false) {
            throw new RuntimeException('Bcrypt hashing not supported.');
        }

        return $hash;
    }

其中password_hash创建密码哈希,password_hash()使用强大的单向哈希算法创建新的密码哈希。password_hash()与 crypt()兼容。因此, crypt()创建的密码哈希可以与 password_hash()一起使用 。

string crypt (string $str [,string $salt ])

该salt参数是可选。然而,crypt()创建了一个弱哈希salt。PHP 5.6或更高版本会引发E_NOTICE错误。

string password_hash (string $passwordinteger $algo [array $options ]

$algo:

PASSWORD_DEFAULT - 使用bcrypt算法(默认为PHP 5.5.0)。请注意,随着新增和更强大的算法被添加到PHP,此常数旨在随着时间的推移而改变。因此,使用此标识符的结果的长度可能随时间而变化。因此,建议将结果存储在可以扩展超过60个字符的数据库列中(255个字符将是一个不错的选择)。
PASSWORD_BCRYPT- 使用CRYPT_BLOWFISH算法创建哈希。这将使用“ 2y ”标识符生成标准的crypt()兼容哈希值。结果将始终为60个字符的字符串,或FALSE失败。

支持的选项:

盐 - 在散列密码时手动提供盐。请注意,这将覆盖并防止自动生成盐。

如果省略,每个密码散列将由password_hash()生成一个随机盐。这是预期的操作模式。

警告
从7.0.0开始,salt选项已被弃用。现在最好简单地使用默认生成的盐。
成本 - 表示应该使用的算法成本。这些值的示例可以在crypt()页面上找到。

如果省略,将使用默认值10。这是一个很好的基准成本,但您可能会考虑根据您的硬件来增加它。

在Illuminate\Hashing\BcryptHasher中:

//设置默认值
protected $rounds = 10;
public function setRounds($rounds)
{
        $this->rounds = (int) $rounds;
        return $this;
}

//如果$options['rounds']为空  使用默认值
protected function cost(array $options = [])
{
    return isset($options['rounds']) ? $options['rounds'] : $this->rounds;
}

这样成本默认为10;

猜你喜欢

转载自blog.csdn.net/weixin_38674371/article/details/76112513
今日推荐