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

689
Views
Inconsistent hash with Spring Boot BCryptPasswordEncoder matches() method

In my project, I'm using BCryptPasswordEncoder as a way to encode my passwords. As long as I don't shut my machine off, everything works fine, however, when re-running my code, BCryptpasswordEncoder gets re-initialized as well, resetting its hash, making the matches() method not work with the passwords in my storage, which were created with the previous hash.

Here is the piece of code I am talking about:

        PasswordEncoder encoder = new BCryptPasswordEncoder();
        User u = this.dataSource.getUserByUsername(username);
        String passwordEncoded = encoder.encode(password);
        if (u == null) {
            return "No such user";
        } else {
            if (encoder.matches(password, u.getPassword())) {
                return passwordEncoded;
            } else {
                return "Incorrect password";
            }
        }

I know that keeping a consistent hash would defeat the purpose of encoding in general, but the way it is now, shutting anything off renders all my previous user entries in my repository useless. Is there anything I can about this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

BCryptpasswordEncoder automatically salts the passwords. The specific salt that they append to the password is randomly generated every time it is initialized.

When you reinitialize BCryptpasswordEncoder, you are generating a new salt to append to the password, so naturally, the results would be different.

You can find out how to overcome this problem here

over 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!