Quick revision
The short answer
First we protect every stored value at once by wrapping it: a batch job replaces each SHA-1 hash with bcrypt(sha1) and marks the row with its own {id}. A custom encoder checks those rows by applying SHA-1 and then bcrypt, and on each successful login Spring Security re-hashes the real password with the current encoder through UserDetailsPasswordService. Accounts that never log in get a forced reset after a deadline, and we check whether the old hashes ever leaked.
What to remember
In this answer, we moved 3 million unsalted SHA-1 hashes to a modern format without a mass reset. A batch job wrapped every value as bcrypt(sha1) so the whole table is protected immediately, a custom encoder registered under its own {id} verifies wrapped rows, and DelegatingPasswordEncoder with UserDetailsPasswordService re-hashes the real password on each successful login. We finished by forcing resets for the inactive tail and for any accounts whose old hashes may have leaked.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we protect every stored value at once by wrapping it: a batch job replaces each SHA-1 hash with bcrypt(sha1) and marks the row with its own {id}. A custom encoder checks those rows by applying SHA-1 and then bcrypt, and on each successful login Spring Security re-hashes the real password with the current encoder through UserDetailsPasswordService. Accounts that never log in get a forced reset after a deadline, and we check whether the old hashes ever leaked.
Why this question is asked
Unsalted SHA-1 is close to the worst way to store passwords that is still called hashing. Most of those 3 million hashes could be cracked in hours if the table leaked, and the business will still not accept a forced reset for everyone. Interviewers ask this question because it tests whether we know how to improve security today without waiting for users, and whether we know the Spring Security hooks for gradual upgrades. In this answer, we will see why waiting for logins is not enough, wrap the hashes in bcrypt, verify the wrapped values with a custom encoder, upgrade them on login, and then deal with inactive users and possible past leaks.
1Why Waiting for Logins Is Not Enough
The textbook answer is to re-hash each password the next time its owner logs in, because only then do we have the plain password. That is correct, but on its own it leaves a long tail. If 40 percent of users log in during the first month, 1.8 million weak hashes are still sitting in the table, and some users will never come back at all.
SHA-1 without a salt makes this tail dangerous. Identical passwords produce identical hashes, so an attacker sees at a glance that 30,000 users share one value, and precomputed tables and GPU cracking recover common passwords almost immediately. We need a step that protects every row today, before anyone logs in.
2Wrap Every Existing Hash in bcrypt
We cannot recover the passwords, but we do not need to. We can treat the old SHA-1 hex string as if it were the password and run bcrypt over it. The stored value becomes bcrypt(sha1(password)): salted and slow, even though the inner step is weak. An attacker who steals the new table must now do a full bcrypt computation for every guess for every user.
Notice the cost. At cost 12 one bcrypt hash takes roughly 250 milliseconds on one core, so 3 million rows need about 200 hours of CPU. Spread over 32 cores that is under seven hours, which is fine for a background job. We also delete the old values from backups and replicas on their normal schedule, because the batch only helps if the bare SHA-1 values stop existing.
3Verify Wrapped Values With a Custom Encoder
Now the login code has to understand two formats: rows already upgraded to plain bcrypt and rows still wrapped. DelegatingPasswordEncoder handles this for us if we give the wrapped format its own id. Its encoder takes the typed password, applies SHA-1 to produce the same hex string the old system stored, and then lets bcrypt compare it with the wrapped value.
The {id} prefix is what keeps this clean. Each row says which format it uses, so there is no flag column to keep in sync and no guesswork in the code. One detail matters: the hex string must be formatted exactly as the old system did, usually lower case. If it used upper case, our encoder must too, or every legacy login will fail. A test with a few known password and hash pairs from the old system catches that before release.
4Upgrade to Plain bcrypt on Successful Login
When a user with a wrapped row logs in, we have the real password for a moment, so we can store a proper hash of it. Spring Security does this automatically when two things are true: the PasswordEncoder says the stored value should be upgraded, and our user details service also implements UserDetailsPasswordService.
DelegatingPasswordEncoder.upgradeEncoding() returns true whenever the stored id is not the default id, so every {sha1-bcrypt} row qualifies. After a successful password check, DaoAuthenticationProvider encodes the raw password with the default encoder and calls updatePassword().
- Load the user, read the {id}sha1-bcrypt
- SHA-1 the typed password, bcrypt-compare
Sha1BcryptPasswordEncoder - Password correct?YesContinueNoReject the login
- upgradeEncoding() true?YesEncode with bcrypt, call updatePassword()NoDone
- Row now {bcrypt}the legacy step is gone for this user
5 steps in order. Play them, or step through them one at a time.
Notice that the user sees nothing. They type their usual password, log in, and their row moves to the modern format in the same request.
5Deal With the Tail and With Past Leaks
We count rows per prefix every week. When the number of {sha1-bcrypt} rows stops falling, we set a deadline, for example six months after launch, after which those accounts are flagged for a forced reset by email on their next visit. The wrapped hashes are already safe to keep, so the deadline is about cleaning up code, not about urgent risk.
There is one more question we must ask: were the old SHA-1 hashes ever exposed? Old backups, a past breach, a database dump shared with a vendor. If yes, wrapping does not help, because an attacker may already have cracked them. In that case we force resets for all affected accounts, starting with the most valuable ones, and we can check new passwords against lists of known breached passwords. Spring Security offers a CompromisedPasswordChecker with an implementation that queries a breached-password service in recent versions (check your version).
6Follow-up questions
Is bcrypt over a SHA-1 hash weaker than bcrypt over the password?
Slightly, in theory, but not in a way that matters here. The attacker still has to run bcrypt per guess, which is what makes cracking expensive. The wrapped format is a stepping stone; it is replaced by plain bcrypt as soon as the user logs in.
Why give the wrapped format its own id instead of reusing {bcrypt}?
Because the check is different. A {bcrypt} row is checked against the raw password, a wrapped row against SHA-1 of it. Without a separate id the encoder could not tell which steps to apply, and upgradeEncoding() would not know that the row still needs upgrading.
Could we use Argon2 instead of bcrypt for the wrapping?
Yes. Any slow, salted function works. bcrypt is a common choice because it needs no extra library in Spring Security, while Argon2PasswordEncoder needs Bouncy Castle on the classpath. The important part is that the wrapping and the final format both use a slow function.
7Conclusion
In this answer, we moved 3 million unsalted SHA-1 hashes to a modern format without a mass reset. A batch job wrapped every value as bcrypt(sha1) so the whole table is protected immediately, a custom encoder registered under its own {id} verifies wrapped rows, and DelegatingPasswordEncoder with UserDetailsPasswordService re-hashes the real password on each successful login. We finished by forcing resets for the inactive tail and for any accounts whose old hashes may have leaked.
Open with
I would not rely only on upgrading at login, because unsalted SHA-1 is crackable in hours and many users will not log in for months. Step one is a batch job that wraps every stored hash: it runs bcrypt over the existing SHA-1 hex string and stores the result with its own prefix, say {sha1-bcrypt}.
Then, if they want more
That protects all 3 million rows immediately; at cost 12 it is a few hundred CPU hours, which parallelises easily. Step two is a custom encoder for that prefix: on login it takes the typed password, applies SHA-1 exactly as the old system did, and bcrypt-compares the result.
I register it with DelegatingPasswordEncoder next to the normal bcrypt encoder, which is the default for new passwords. Step three is upgrading on login: DelegatingPasswordEncoder's upgradeEncoding returns true for any non-default prefix, and if my user details service implements UserDetailsPasswordService, DaoAuthenticationProvider re-encodes the raw password with bcrypt and saves it.
I would track the count per prefix, and after a deadline force a reset for accounts still on the wrapped format. Finally I would find out whether the old hashes ever leaked in backups or a past breach, and if so force resets for those accounts and check new passwords against breached-password lists.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You inherit a database of 3 million users whose passwords are stored as unsalted SHA-1...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/16dbec2808/