Showing posts with label yii2 security class. Show all posts
Showing posts with label yii2 security class. 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;
}

Tuesday, April 24, 2018

Yii2 cryptography

Yii2 cryptography

Generating Pseudo random

Pseudo random is helpful in security. You can use Pseudo random data as a token while sending the email for reset password or while sending the confidential data on the server.

$securityKey = Yii::$app->getSecurity()->generateRandomString();

You can also pass the integer value in generateRandomString() function.
For Example:

$securityKey = Yii::$app->getSecurity()->generateRandomString(10);

Recent Update

yii2 session handling

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

Most Search