How to encrypt a password in cakephp 2.x version

2019-07-18 13:20发布

问题:

Hello everyone i am using cakephp 2.x, as i am new to here, i need to encrypt my password before it stores to database

User.ctp : I am posting like this to post

<?php
  echo $this->Form->input('password',array('type'=>'password','label'=>false,'div'=>false,'class'=>'form-control','id'=>'password'));
?>

Controller:

public function setting()
{

    $this->layout='setting_template';
    if($this->Session->read('username')==""){

        $this->redirect(array('action' => 'user_login'));

    }
    elseif ($this->Session->read('username') == "admin" )
    {

        if($this->request->is('post'))
        {
            $this->data['password'] = encrypt($this->data ['password']);

            if ($this->Login->save($this->request->data)) {
                $this->Session->setFlash('The user has been saved');
                $this->redirect(array('action' => 'setting'));
            } else {
                $this->Session->setFlash('The user could not be saved. Please, try  again.');
            }
            }
        $opp=$this->Login->find('all');
        $this->set('login',$opp);

    }
    else{

        echo "<script type='text/javascript'> alert('Permission Denied');    </script>";
        $this->redirect(array('action' => 'index'));

    }

}

Login controller:

public function login()
{
$this->layout='login_template';
if($this->data)
{
$this->Session->write('id',$this->data['Login']['id'] );
$results = $this->Login->find('first',array('conditions' =>  array('Login.password' => $this->data['Login']['password'],'Login.username'  => $this->data['Login']['username'])));
$this->Session->write('name',$results['Login']['name']);
if ($results['Login']['id'])
 {
 $this->Session->write($this->data['Login']['username'].','. $this->data['Login']['password']);
   $this->Session->write('username',$this->data['Login']['username']);
   $this->redirect(array('action'=>'index'));
   }
  else
  {
   $this->Session->setFlash("error");
 }
}

How can i encrypt the password file and also how can use the Model

回答1:

As you are using CakePhp go with framework's best practices.

When creating new user records you can hash a password in the beforeSave callback of your model using appropriate password hasher class:

App::uses('SimplePasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {
   public function beforeSave($options = array()) {
        if (!empty($this->data[$this->alias]['password'])) {
        $passwordHasher = new SimplePasswordHasher(array('hashType' => 'sha256'));
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
            $this->data[$this->alias]['password']
            );
        }
        return true;
    }
 }

You don’t need to hash passwords before calling $this->Auth->login(). The various authentication objects will hash passwords individually.

If you are using different model than User for authentication you need to define that in AppController. In your Case you need to do something like this in AppController:

$this->Auth->authenticate = array(
'Form' => array('userModel' => 'Login')
);

If you wish to hash your password, try this:

$hashedPassword = AuthComponent::password('original_password');

See Here :Cakephp Password Hashing.



回答2:

how about

MD5($this->data['password']);  

on controller ?