I’ve applied a small modification to the Portable PHP password hashing framework, so it can be easily used in CodeIgniter projects. An example of using it to authenticate users:
$this->load->library( 'PasswordHash' ); $query = $this->db->query(" SELECT `user_id`,`password` AS `hash` FROM `user` WHERE `username` = ". $this->db->escape($username) ." LIMIT 1 "); // check to see whether username exists if ( $query->num_rows() == 1 ) { $row = $query->row(); if ( $this->passwordhash->CheckPassword( $password, $row->hash ) ) { return $row->user_id; } }
To generate a hashed password:
$this->load->library( 'PasswordHash' ); $password = ( isset( $_POST['password'] ) ? $_POST['password'] : '' ); if ( $password ) { $hash = $this->passwordhash->HashPassword( $password ); if ( strlen( $hash ) < 20 ) { exit( "Failed to hash new password" ); } }
For more details, please check out the repository on GitHub: github.com/glenscott/passwordhash-ci