CodeIgniter Forums
hash function not working at all... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: hash function not working at all... (/showthread.php?tid=30854)



hash function not working at all... - El Forum - 05-27-2010

[eluser]Unknown[/eluser]
can someone please tell me why this is not working?

Code:
class User_Model extends Model {
    
    function validateLogin() {
        $this->db->where('email', $this->input->post('email'));
        $this->db->where('hash', hash('sha512', $this->input->post('password')));
        $q = $this->db->get('login');
        if($q->num_rows() == 1) {
            return true;
        }
    }
}

I created a script outside the CI installation and used the hash function successfully there, but I can't inside the model, why? Sad


hash function not working at all... - El Forum - 05-28-2010

[eluser]Clooner[/eluser]
Probably the input isn't exactly the same. Try using the trim command to remove any spaces from the password and compare the two inputs. Also it is a good idea to salt the passwords with an extra secret key


hash function not working at all... - El Forum - 05-28-2010

[eluser]danmontgomery[/eluser]
What does "not working" mean?


hash function not working at all... - El Forum - 05-28-2010

[eluser]Clooner[/eluser]
and also you can't call $this->input->post('password') from the model directly. Pas it as a variable in the function


hash function not working at all... - El Forum - 05-28-2010

[eluser]mddd[/eluser]
@clooner: That is not true. You can use $this->input in models just fine.

I do agree with your earlier remark. Gjore.S should check if the provided data comes through correctly.

And, as noctrum says, it would be good to have more information on exactly what goes wrong.
Just saying "it doesn't work" is not enough.
For instance, if there are two records in the database with the correct email address and password, the query will give 2 results.
And therefore login validation will fail. The problem could be in other places than the hash() function or even the entire model.


hash function not working at all... - El Forum - 05-28-2010

[eluser]Unknown[/eluser]
I got it fixed, it turned out that the login form wasn't posting input to the correct controller.

About the hashing, I use triple salting in production-ready code, this one was just for testing.

something like:
Code:
$pass = hash('sha512', $pass.$salt1.$pass);
$pass = hash('sha512', $salt2.$pass.$pass);
$pass = hash('sha512', $pass.$pass.$salt3);