A
A
asferot2018-12-11 12:12:38
Yii
asferot, 2018-12-11 12:12:38

How to properly hash passwords?

Found many solutions, but not complete. What is the best way to hash passwords and how to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Edward, 2018-12-11
@edvardpotter

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

Where $password is your password
asferot , if you are registering, then the steps should be something like this:
1. The user on the site enters the form data.
2. A user model is created (let's say User has email and password properties )
$model = new User();
$model->emal = '[email protected]';
$model->setPassword(123456);
$model->save();

In the User model, you need to add a method - setPassword :
public function setPassword($password)
{
    $this->password = Yii::$app->security->generatePasswordHash($password);
}

In this method, you pass the password, yii hashes it, and saves it to the $password property of the User model.
As a result, you will not store the original password in the database, but the hashed one.

B
Boris Syomov, 2018-12-11
@kotomyava

If it is yii1.x then php.net/manual/ru/function.password-hash.php using bcrypt.
If it is yii2.x, then there is already a built-in wrapper for this https://www.yiiframework.com/doc/api/2.0/yii-base-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question