Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

391
Views
How to compare passwords (Bcrypt hashes) in Symfony 3?

In setting up a "change password" feature for a site I have a secondary password entry (where you need to enter your password again before you can change it).

I need to be able to check the user's current password (hashed using Bcrypt) against the password that has been entered.

In my controller action I have:

$currentPassword = $request->request->get('password');
$encoder = $this->container->get('security.password_encoder');
$encodedPassword = $encoder->encodePassword($user, $currentPassword);

if($encodedPassword == $user->getPassword()) { // these don't ever match.
    // ...
}

encodePassword(...) produces a digest of the password that was entered, but it's not the same as the saved password (the plaintext is the same), so I'm thinking that a different salt is being applied and therefore producing the mismatch.

Since Bcrypt incorporates the salt in the password digest, I'm not saving it anywhere.

How can I check if the entered plaintext password matches the stored Bcrypt digest in Symfony 3?

I am not using FOSUserBundle.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can compare the $currentPassword password with the stored one using the isPasswordValid method of the encoder service:

$encoderService = $this->container->get('security.password_encoder')

and then pass the user object as first argument of the method:

$match = $encoderService->isPasswordValid($userObject, $currentPassword)

that will returns true if the comparison match or false otherwise.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!