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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 250
    Comment on it

    Hello Readers ,

     

    Their are many methods to hash password in cakephp like cakephp alias , md5 etc blowfish is also one of them . One thing we have to note down that we can’t use Blowfish if we already have a database filled with passwords hashed using another method.

    In the AppController write this code .

     

    1. <?php
    2. class AppController {
    3.  
    4. public $components = array(
    5. 'Auth' => array(
    6. 'authenticate' => array(
    7. 'Form' => array(
    8. 'passwordHasher' => 'Blowfish'
    9. )
    10. )
    11. )
    12. );
    13. }


    Use before save method to hash the password . In the User Model.

     

    1. <?php
    2. App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
    3.  
    4. class User extends AppModel {
    5.  
    6. public function beforeSave($options = array()) {
    7. // if ID is not set, we're inserting a new user as opposed to updating
    8. if (!$this->id) {
    9. $passwordHasher = new BlowfishPasswordHasher();
    10. $this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);
    11. }
    12. return true;
    13. }
    14. }


    In the User Controller .

     

    1. <?php
    2. class UsersController extends AppController {
    3.  
    4. public function login() {
    5. if ($this->request->is('post')) {
    6. if ($this->Auth->login()) {
    7. return $this->redirect($this->Auth->redirectUrl());
    8. } else {
    9. $this->Session->setFlash( __('Username or password incorrect'));
    10. }
    11. }
    12. }
    13. }

     

    And that’s all there is to it.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: