在yii2中提供了密码加密以及验证的一系列方法,方便我们的使用,它使用的是bcrypt算法。查看源码我们可以发现它使用的是php函数password_hash()和crypt()生成。

加密:

/**
 * $password  要加密的密码
 * $hash  加密后的hash字符串
 */
$hash = yii::$app->getsecurity()->generatepasswordhash($password);

验证密码:

/**
 * $password  要验证的明文密码
 * $hash   加密后的hash字符串
 */
yii::$app->getsecurity()->validatepassword($password, $hash);

总结

以上所述是www.887551.com给大家介绍的yii2处理密码加密及验证的方法,希望对大家有所帮助