Unable to login while using salt for password hashing in cakephp3x

64 Views Asked by At

I have 2 fields password and salt in my users table. While saving the data, its working fine & data are being saved into both columns using the following code.

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

$password is having the following array value.

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

I am attaching my login code below. Here I have manually checked for the password and salt. But its not working.

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

There are 0 best solutions below