简体   繁体   English

如何通过哈希使其确认密码验证cakephp

[英]how to make confirm password validation cakephp with hashing it

I'm using cakephp 2.xx, I want to hashing password with sha256 before it going to database, before it I wanna make validation value password in my form input, validation which check password input and re-confirm password is match, if In my controller, when form catch validation, the password automatically hash 我正在使用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']);
}

necessarily, the password hash when form no catch validate at all, so how can I make validation in my model ? 必然,当没有捕获时,密码哈希值将全部验证,那么如何在模型中进行验证?

Thanks In Advance. 提前致谢。

In your model (Driver.php) 在您的模型中(Driver.php)

Validation 验证

<?php 
    public $validate = array(

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

Custom validation rule 自定义验证规则

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

Hashing,but I think that better to choose AuthComponent 散列,但我认为最好选择AuthComponent

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

It's overall description and you probably would need to modify some parts of it 它是整体描述,您可能需要修改其中的某些部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM