Password is not verified using function password_verify

前端 未结 4 1719
轮回少年
轮回少年 2020-12-12 05:36

I think i have hashed password using function PASSWORD directly from mysql database(am i doing wrong here?). And i am trying to verify that password with this c

4条回答
  •  醉梦人生
    2020-12-12 06:03

    One cannot search for a salted password hash in a database. To calculate the hash you need the password_hash() function as you already did correctly in your insert statement.

    // Hash a new password for storing in the database.
    // The function automatically generates a cryptographically safe salt.
    $hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT);
    

    To check a password, you first need to search by username only (used a prepared query to avoid sql injection):

    $sql = 'select * from admin where username = ?';
    $db->prepare($sql);
    $db->bind_param('s', $first);
    

    When you finally got the stored hash from the database, it can be checked like this:

    // Check if the hash of the entered login password, matches the stored hash.
    // The salt and the cost factor will be extracted from $existingHashFromDb.
    $isPasswordCorrect = password_verify($password, $existingHashFromDb);
    

提交回复
热议问题