Showing posts with label hash password in yii2. Show all posts
Showing posts with label hash password in yii2. Show all posts

Monday, April 30, 2018

yii2 encrypt decrypt

Yii2 encrypt decrypt data

Yii2 have inbuilt functions for encrypt/decrypt data using secret key. You need secret key to decrypt the data. So if the person have secret key he/she will be decrypt the data easily using the getSecurity() method. Below example is showing the data encrypt/decrypt:

$data = 'FORM DATA';
$secretKey = 'YOUR SECRET KEY'
$encryptData = Yii::$app->getSecurity()->encryptByPassword($data, $secretKey);

$data = Yii::$app->getSecurity()->decryptByPassword($encryptData, $secretKey);

yii2 decrypt password

Password Decryption in Yii2

Yii2 have inbuilt functions for encrypt/decrypt data using secret key. Yii2 getSecurity() method return the pseudo random data which is useful for encrypt/decrypt your hash data. The given example describe the validate password and set password methods:

public $key = 'SECRET KEY';
public function setPassword($pwd)
{
    $this->password_hash = Yii::$app->getSecurity()->encryptByPassword($pwd, $this->key);
}
public function validatePassword($pwd)
{
    $decryptPassword = Yii::$app->getSecurity()->decryptByPassword($this->password_hash, $this->key);
    return $decryptPassword === $pwd;
}

Recent Update

yii2 session handling

use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...

Most Search