Search code examples
yii2yii2-extension

Yii2-user Dektrium - how to hash password


i want to do hash password and check that with database ( password_hash ) How can I do it????

        $username = $auth['username'];

my password is

 $password = $auth['password'];

i want hash that :

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

Solution

  • You could generate the $hash using

    $hash = Yii::$app->getSecurity()->generatePasswordHash($password);
    
    
    $find = \dektrium\user\models\User::findOne(['username' => $username, 
          'password_hash' => $hash]);
    

    Th code belowe is from dektrium/yii2-user/helpers/password.php ( the code for hash function ..of dektrium adn as you see the extensions use the generatePasswordHash and a cost

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

    default cost = 8