Wednesday, October 23, 2013

Password encryption in codeigniter with zend crypt module

Integrating zend with codeigniter is described here.
Now in Zend.php create a function, say encryptPassword and put the following code in it.

public function encryptPassword($pass){
$bcrypt = new Zend\Crypt\Password\Bcrypt();
$securePass = $bcrypt->create($pass);
return $securePass;


Now from your controller you can access this function using the following code:

$this->load->library('zend');
$enc = $this->zend->encryptPassword('helloWorld!');

To verify or check the password create another function in Zend.php, say verifyPassword and put the following code in it.

public function verifyPassword($passwordTextToCheck,$encryptedPass){
$bcrypt = new Zend\Crypt\Password\Bcrypt();
return $bcrypt->verify($passwordTextToCheck, $encryptedPass);

}

Now from your controller you can access this function using the following code:

$this->load->library('zend');
$encryptedPassFromDB = '$2y$14$aR1YyFbg/UobJBzrLUJbPe57CfFWo300rT6zWBdh7Jj1bdcayN1im=';
$isValid = $this->zend->verifyPassword('helloWorld!',$encryptedPassFromDB);

Keep in mind that the verifyPassword function returns Boolean.

No comments:

Post a Comment