如何通过散列

时间:2016-08-12 07:57:14

标签: php validation cakephp hash sha256

我正在使用cakephp 2.xx,我想在进入数据库之前使用sha256散列密码, 在它之前我想在我的表单输入中验证值密码,验证哪个检查密码输入并重新确认密码匹配,如果在我的控制器中,当表单捕获验证时,密码自动哈希

if ($this->request->data['Driver']['password'] != $this->request->data['Driver']['confirm_password']) {
      $this->request->data['Driver']['password'] = hash('sha256',$this->request->data['Driver']['password']);
}

必然,表单没有catch验证时的密码哈希,那么如何在我的模型中进行验证呢?

提前致谢。

1 个答案:

答案 0 :(得分:4)

在您的模型中(Driver.php)

验证

<?php 
    public $validate = array(

        'password' => array(
            'notempty' => array(
                'rule' => array('notempty'),                            
            ),
            'password_confirm'=>array(
                'rule'=>array('password_confirm'),
                'message'=>'Password Confirmation must match Password',                         
            ),    
        ),      
    );
?>

自定义验证规则

<?php 
    public function password_confirm(){ 
        if ($this->data['Driver']['password'] !== $this->data['Driver']['password_confirmation']){
            return false;       
        }
        return true;
    }
?>

哈希,但我认为最好选择AuthComponent

<?php 
    public function beforeSave($options = array()) {        
        $this->data['Driver']['password'] = hash('sha256',$this->data['Driver']['password']);   
        return true;        
    }
?>

这是整体描述,你可能需要修改它的某些部分