2

我想做哈希密码并用数据库检查(password_hash)我该怎么做????

        $username = $auth['username'];

我的密码是

 $password = $auth['password'];

我想要哈希:

 $find = \dektrium\user\models\User::findOne(['username' => $username, 'password_hash' => $password]);
4

2 回答 2

2

您可以使用生成 $hash

$hash = Yii::$app->getSecurity()->generatePasswordHash($password);


$find = \dektrium\user\models\User::findOne(['username' => $username, 
      'password_hash' => $hash]);

下面的代码来自 dektrium/yii2-user/helpers/password.php (哈希函数的代码 ..of dektrium 和你看到的扩展使用 generatePasswordHash 和成本

public static function hash($password)
{
    return \Yii::$app->security->generatePasswordHash($password,
      \Yii::$app->getModule('user')->cost);
}

默认成本 = 8

于 2017-03-13T10:15:38.433 回答
0

我知道回答这个问题已经很晚了,但是对于那些仍在寻找的人。我最近遇到了这个问题,经过大量测试,下面的代码对我有用:

$behaviors['authenticator'] = [
        'class' => HttpBasicAuth::className(),
        'auth' => function ($username, $password) {
            $user = \dektrium\user\models\User::findOne(['username' => $username]);
            if ($user->validate($password)) {
                return $user;
            }
            return null;
        }
    ];
于 2020-07-27T03:24:18.230 回答