Using PHPass Library in CodeIgniter

Date Posted: December 8, 2014, 7:11 am


Before we start integrating PHPass in your CodeIgniter Project, download the file bellow:

Download PHPass

Then upload the phpass folder in your “application/libraries” directory.

Integration

Go to application/config/autoload.php then locate the code:

1
$autoload['libraries'] = array();

Then add the phpass library so that it gets autoloaded every time CodeIgniter is accessed.

1
$autoload['libraries'] = array('phppass/passwordhash');

If you do not want to autoload PHPass then you can construct the library in your Model or Controller like this:

1
$this->load->library('phppass/passwordhash');

Using the Library

PasswordHash class adds salt to the password and hashes it with 8 passes of MD5. MD5 is used by default because it’s supported on all platforms.

To generate a password you can do this:

1
echo $this->passwordhash->HashPassword('test');

Comparing plain password and hashed password:

1
2
3
4
5
6
7
8
$password_hashed = '$P$B55D6LjfHDkINU5wF.v2BuuzO0/XPk/';
$plain_password = 'test';

if($this->passwordhash->CheckPassword($plain_password, $password_hashed)) {
    echo "YES, Matched";
} else {
    echo "No, Wrong Password";
}

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me