Yii2 Method for Cryptographic Encryption and Verification

  • 2021-12-11 07:13:08
  • OfStack

In Yii2, we provide a series of methods of password encryption and verification, which is convenient for us to use. It uses bcrypt algorithm. Looking at the source we can see that it is generated using the PHP functions password_hash () and crypt ().

Encryption:


/**
 * $password   Password to encrypt 
 * $hash   Encrypted hash String 
 */
$hash = Yii::$app->getSecurity()->generatePasswordHash($password);

Authentication password:


/**
 * $password   The plaintext password to verify 
 * $hash    Encrypted hash String 
 */
Yii::$app->getSecurity()->validatePassword($password, $hash);

Summarize

The above is the site to introduce the Yii2 password encryption and verification methods, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: