i am currently working on my own user class with login and register functions. I am doing this mainly for the learing experience. Now i am having a problem wich i am unable to resolve.
My register function looks like this:
public function createUser($username, $email, $password)
{
$password = trim($password);
//Generate users salt
$user_salt = $this->getRandomHash();
//Salt and Hash the password
$password = $user_salt . $password;
$password = $this->hashData($password);
//Create verification code
$code = $this->randomString();
//Commit values to database here.
$created = "INSERT INTO users (username, email, password, user_salt, is_verified, is_active, verification_code) VALUES ('".$username."','".$email."', '".$password."', '".$user_salt."', 0, 1, '".$code."')";
$created = $this->_database->Set($created);
if($created != false){
return true;
}
return false;
}
while hashData looks like this:
protected function hashData($data)
{
return hash_hmac('sha512', $data, $this->_siteKey);
}
and this is my login function:
$password = trim($password);
//Select users row from database base on $email
$selection = "SELECT * FROM users WHERE username='".$username."' LIMIT 1";
$selection = $this->_database->Get($selection);
$row = mysqli_fetch_assoc($selection);
//Salt and hash password for checking
$password = $row['user_salt'] . $password;
$password = $this->hashData($password);
$match = false;
//Check email and password hash match database row
if($username == $row['username'] && $password == $row['password']){
$match = true;
}
Now the problem is that the passwords do not match even though i am hundret percent sure im typing it correctly. I am not quite sure why it fails because i am using the same salt to hash the typed in password. Maybe i am doing something completly wrong here…If you need more code please let me know. Otherwise thanks to anyone willing to help.
Stupid mistake on my part!
The salt column in the database was only accepting a salt with the length of 50. My generated salt was way longer..