How to use CPasswordHelper in yii2

271 views Asked by At

I am trying to encrypt a password using the PHP crypt function. In yii, it's recommended to use CPasswordHelper. When I call CPasswordHelper::hashPassword($password), it says Class 'common\models\CPasswordHelper' not found.

Please help?

1

There are 1 answers

1
Bizley On

To generate hashed password in Yii 2 use security component.

You can generate password hash with:

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

To validate this password later on you can check:

if (\Yii::$app->getSecurity()->validatePassword($rawUserPassword, $hash) {}

Read more about this in the Guide.