Password verify always returning true - php

I've been trying to write a simple login for a couple of days now. After I'd thought I had it working, I realized that it would accept any input in the password field as being true so I scrapped it and started again. I'm trying to use the php function password_verify for the verification but no matter what I do, it always returns true still. Is there something I'm doing wrong? Here is my code (I know it's not secure, I just want it to recognize a wrong password for now)
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$hash = password_hash($password, PASSWORD_DEFAULT);
if(password_verify($_POST['password'], $hash))
{
echo 0;
}
else
{
echo 1;
}
}

The reason it always returns true is because you are verifying a hash that you just created... it will always be verified correctly.
When you use the password_verify() function the $hash parameter has to come from somewhere else (usually a database of some kind).
// If this is a POST request then handle the form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get password from form
$pass = filter_input(INPUT_POST, 'password', FILTER_UNSAFE_RAW);
// Connect to a database of some kind
// Get a previously hashed password
$hash = 'A HASH FROM SOMEWHERE ELSE...';
// Verify the previously hashed password
// against the password provided by the user
if (password_verify($pass, $hash)) {
echo 'Password is valid!';
}
}

You are $password getting a $_POST['password'];
You are hash a $password which is $_POST['password'];
password_verify($_POST['password'], $hash)
You are comparing a $_POST['password'] with a hash. the hash is also $_POST['password'].
That's why they return always true.because the $passwod,hash are $_POST['password'] is same.

You check the post password with the post password. You should check the post password instead with a wanted password.

You're assigning $hash to the password you received through POST.
This is how password_verify works
boolean password_verify ( string $password , string $hash )
Verifies that the given $hash matches the password received.
So now you're checking the password stored in $hash with the password obtained in POST which are the same.
Hence always true.

Related

login using PHP data object [duplicate]

I'm using PHP's password hashing API to hash and verify my passwords on a site I'm building, however whenever I try and verify my password it always returns false.
I have a User class which sets the password before they are inserted into the database:
public function set__password($passwd) {
self::$password = password_hash($passwd, PASSWORD_BCRYPT, array('cost' => 12));
}
If the username and email is unique the new user row is inserted - upon checking my database I have what seems to be a valid BCRYPT string for my password:
$2y$12$lTMEP0wevDEMX0bzStzoyOEzOTIAi3Hyhd3nYjGwzbI
To verify my password, I run the following script:
$username = $_POST['username'];
$password = $_POST['password'];
$DB = Database::getInstance();
// Get the stored password hash
$res = $DB->run__query('SELECT password FROM users WHERE username = "' . $username . '"');
$hash = $res[0]['password'];
// Do the passwords match?
if(password_verify($password, $hash)) {
echo 'success';
} else {
echo 'failed';
}
$hash pertains to the string quoted above, however when I then call password_verify($password, $hash) where $password is the plain-text password retrieved from my input field, I always receive a value of false.
The given hash string example has 50 characters instead of 60. Double-Check the database - CHAR(60) - and var_dump($hash).
Other problem that you can have, is when you reduce the cost in the server for gaining time.
Always use password_hash($pass, PASSWORD_DEFAULT), is the best way.

Password_verify says incorrect password

I am coding a Login in PHP the thing is it says the password is incorrect even though I know it is correct.
I assign my variables using this
$password = trim($_POST['password']);
$user_password = $user['password']; //Hashed password from database
$salt = '-45dfeHK/__yu349#-/klF21-1_\/4JkUP/4';
$hashed_password = hash(sha256, $password . $salt);
I check the password through
if(password_verify($user_password, $hashed_password)){}
but it returns false but if I use
if($user_password == $hashed_password){}
it works fine.
Edit:
$password = trim($_POST['password']);
$user_password = $userdata['password'];
if(password_verify($password, $user_password)){}
If $user_password was created with hash() it wont work, it needs to be created with crypt() or preferably password_hash. If your 'stuck' with your current hash() created passwords, you simply cant use password_verify but i would encourage you to switch to this system asap.
password_verify expects the users password to be unhashed https://secure.php.net/manual/en/function.password-verify.php
$user_password=trim($_POST['password'])
if(password_verify($user_password, $user['password'])){}
it does all the rehashing for you.

password_verify returning false even though correct

Hi I am testing using bcrypt with php after using it with node.js and am finding some trouble with my code. Essentially I am typing in a username and a password into a form and this is hashing and echoing the hash onto the page and it also is running a password verify and is supposed to return a message saying the password is correct if it is correct, but it is not returning true ever.
Here is my code (note there is no use of a database and this code should always mean the $password is equal to the $hash)
<?php
$username = $_POST["name"];
$password = $_POST["pass"];
$hash = password_hash($password, PASSWORD_BCRYPT) . "<br/>";
echo $hash;
if (password_verify($password , $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}
?>
This is returning the has and invalid password as such":
"$2y$10$yzY0md5wm3nBZvuynPV7mO2W3Ux9455AG/dWfLzwEqRtC1DfMx9Oa
Invalid password."
Change
$hash = password_hash($password, PASSWORD_BCRYPT) . "<br/>";
to
$hash = password_hash($password, PASSWORD_BCRYPT);

PHP Password verify always returns false

I'm using PHP's password hashing API to hash and verify my passwords on a site I'm building, however whenever I try and verify my password it always returns false.
I have a User class which sets the password before they are inserted into the database:
public function set__password($passwd) {
self::$password = password_hash($passwd, PASSWORD_BCRYPT, array('cost' => 12));
}
If the username and email is unique the new user row is inserted - upon checking my database I have what seems to be a valid BCRYPT string for my password:
$2y$12$lTMEP0wevDEMX0bzStzoyOEzOTIAi3Hyhd3nYjGwzbI
To verify my password, I run the following script:
$username = $_POST['username'];
$password = $_POST['password'];
$DB = Database::getInstance();
// Get the stored password hash
$res = $DB->run__query('SELECT password FROM users WHERE username = "' . $username . '"');
$hash = $res[0]['password'];
// Do the passwords match?
if(password_verify($password, $hash)) {
echo 'success';
} else {
echo 'failed';
}
$hash pertains to the string quoted above, however when I then call password_verify($password, $hash) where $password is the plain-text password retrieved from my input field, I always receive a value of false.
The given hash string example has 50 characters instead of 60. Double-Check the database - CHAR(60) - and var_dump($hash).
Other problem that you can have, is when you reduce the cost in the server for gaining time.
Always use password_hash($pass, PASSWORD_DEFAULT), is the best way.

Implement crypt() in php

I am trying to encrypt the given password to match one that is in a database. However using crypt() gives me a different result each time so it never matches. How can i make this work.
here is the statement that hashes the password given by the user.
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = crypt($_POST['password']);
prior to this i manually made a user that had the crypt('password') but if I enter 'password' into the field it doesn not match.
Try below:
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
// get the hashed password from database
$hashed_password = get_from_db($username);
if (crypt($password, $hashed_password) == $hashed_password) {
echo "Password verified!";
}
}
Try like this,
//$pass_entered_from_login is the user entered password
//$crypted_pass is the encrypted password from the
//database or file
if(crypt($pass_entered_from_login,$crypted_pass)) == $crypted_pass)
{
echo("Welcome to my web site.")
}
Read more
crypt auto generates the salt each time you use it ............
so use the same salt for the user
do this while registering the user to your database and while checking tooo.
if (empty($_POST) === false) {
$username = $_POST['username'];
$password = crypt($_POST['password'],$_POST['username']);
}
NOTE: the 2nd parameter is the salt in crypt function .
hope it helps :)

Categories