在cakephp3x中使用salt进行密码散列时无法登录

时间:2016-02-11 10:14:25

标签: cakephp login cakephp-3.0 saltedhash

我的用户表中有2个字段的密码和salt。在保存数据的同时,它的工作正常和使用以下代码将数据保存到两列中。

$password = $this->Custom->hashSSHA();
$user->password = $password['encrypted'];
$user->salt = $password['salt'];

$password具有以下数组值。

Array
(
    [salt] => 993ffb0265
    [encrypted] => 8FdzWdZ5jXN3GC+GzkO9p6ee/nk5OTNmZmIwMjY1
)
$password['encrypted'] = 8FdzWdZ5jXN3GC+GzkO9p6ee/nk5OTNmZmIwMjY1993ffb0265.

我在下面附上我的登录代码。在这里,我手动检查了密码和盐。但它不起作用。

public function login() {
        $this->viewBuilder()->layout('');
        if ($this->request->is('post')) {
            $data = $this->request->data;            
            $query = $this->Users->find('all')
                    ->select(['Users.id', 'Users.email', 'Users.name', 'Users.password', 'Users.salt', 'Users.is_active'])
                    ->where(['Users.email' => $data['email'], ['Users.is_active' => 1]]);
            $row = $query->first();           
            $number_login = $query->count();

            if ($number_login != 0) {
                $password = $this->Custom->checkhashSSHA($row['salt'], $data['password']);

                if ($password == $row['password']) {
                    $session = $this->request->session();
                    $session->write('User_name', $row['name']);
                    $session->write('User_email', $row['email']);
                    $session->write('User_id', $row['id']);                   
                    return $this->redirect(['action' => 'index']);                    
                } else {
                    $this->Flash->error(__('Invalid username or password, try again'));
                }
            } else {

                $this->Flash->error(__('Account not activated yet'));
            }

        }
    }

0 个答案:

没有答案