SHA 512 hashing and verifying

Tomislav Tomi Nikolic picture Tomislav Tomi Nikolic · Jan 13, 2017 · Viewed 15.3k times · Source

Alright so Im trying to verify password with SHA 512, but no matter what it still returns false like the hash check is not correct.

Generating hash when registering

$hashed = password_hash(hash('sha512', $password), PASSWORD_DEFAULT);

And to verify (upon login) I use simple

public function isValidLogin($username, $password) {
    $sql = $this->connect();
    $sql->real_escape_string($username);
    $sql->real_escape_string($password);

    $res = $sql->query("SELECT password FROM users WHERE name='".$username."'");

    if ($res->num_rows >= 1) {
        while($row = $res->fetch_assoc()) {
            if (password_verify(hash('sha512', $password), $row['password'])) {
                return true;
            }
        }
    }

    return false;
}

Answer

Geordy James picture Geordy James · Jan 13, 2017

Try this code at time of registering instead of your code.

 $hashed = hash("sha512", $password);