简体   繁体   中英

SHA 512 hashing and verifying

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;
}

在注册时尝试使用此代码而不是您的代码。

 $hashed = hash("sha512", $password);
// original password
$_password = 'bluebeans123';

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

var_dump($password);

$verify = hash('sha512', $_password);
$verify = password_verify($verify, $password);

var_dump($verify);

Elaborate example: http://wiki.travisfont.com/PHP: Passwords (hash_w/_SHA512)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM