Search code examples
phphashphalconsalt-cryptography

SHA512 and custom salt on checkhash - PhalconPHP


public function beforeSave(){
    $salt = "Acrec_$";
    $hashed = hash('sha512', $salt . $this->password);
    $this->password = $hashed;
}

I'm using a custom Salt and custom hash to encrypt the users password, but, now i need to log-in the users.

the Code inside loginAction();

$this->auth->check([
    'email' => $this->request->getPost('email'),
    'password' => $this->request->getPost('password'),
    'remember' => $this->request->getPost('remember')
]);

Solution

  • In phalcon just use:

    $password = $this->request->getPost('password');
    $user->password = $this->security->hash($password);
    

    And

    $password = $this->request->getPost('password');
    $user = Users::findFirst();
    if ($this->security->checkHash($password, $user->password)) {
        // any logic here
    }
    

    By default it's using bcrypt which has salts built-in.