Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement blowfish hashing for passwords encryption in CakePhp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 345
    Comment on it

    Whenever you make registration form in Cakephp all you need to do is to make various field for username , email, password, gender etc etc... If you are inserting these values in the database then you should know that you should encrypt your password so that nobody could read it either you can use md5 encryption or either you can use blowfish hashing. Here we are discussing about the blowfish hashing.

    In your AppController class you should write the below code:

    <?php
    class AppController {
    
        public $components = array(
            'Auth' => array(
                'authenticate' => array(
                    'Form' => array(
                        'passwordHasher' => 'Blowfish'
                    )
                )
            )
        );
    }

    Now to generate a password for password hasher you should write the below code in your model.

    <?php
    App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
    
    class User extends AppModel {
    
        public function beforeSave($options = array()) {
            // if ID is not set, we're inserting a new user as opposed to updating
            if (!$this->id) {
                $passwordHasher = new BlowfishPasswordHasher();
                $this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);
            }
            return true;
        }
    }

    To authenticate your password you don’t really need to do anything because CakePHP have the predefine  function for authentication handler will do the password comparing for you:

    <?php
    class UsersController extends AppController {
    
        public function login() {
            if ($this->request->is('post')) {
                if ($this->Auth->login()) {
                    return $this->redirect($this->Auth->redirectUrl());
                } else {
                    $this->Session->setFlash( __('Username or password incorrect'));
                }
            }
        }
    }

    This is all you need to do encrypt your password using blowfish hashing.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: