Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What does DelegatingPasswordEncoder do, what does a stored value such as {bcrypt}$2a$10$... mean, and why should you not use NoOpPasswordEncoder or plain SHA-256 for passwords?

DelegatingPasswordEncoder is a PasswordEncoder that hands the work to other encoders: it encodes new passwords with its default encoder (bcrypt when created by PasswordEncoderFactories) and, when checking, reads the {id} prefix of the stored value to choose the right encoder. In {bcrypt}$2a$10$..., {bcrypt} names the algorithm, 2a the bcrypt variant and 10 the cost factor. NoOpPasswordEncoder stores plain text and SHA-256 is far too fast, so attackers can test billions of guesses; passwords need a slow, salted, adaptive function.

Why this question is asked

Password storage is one of the few places where a single wrong choice turns a database leak into a leak of every customer's password, including the ones they reuse elsewhere. Interviewers ask this question to check that we understand what Spring Security stores and why, not just that we copy a bean definition. In this answer, we will recap what a password encoder does, see how DelegatingPasswordEncoder chooses an encoder, take a stored bcrypt value apart, and then see why plain text and fast hashes are unsafe and what to use instead.

1A Quick Recap of PasswordEncoder

A PasswordEncoder has two jobs. encode() turns a raw password into the value we store, and matches() checks whether a raw password typed at login fits a stored value. We never decrypt anything: a good encoder is one-way, so the only way to check a password is to encode the typed one again with the same settings and compare.

Two ideas make this safe. A salt is a random value mixed into each password before hashing, so two users with the same password get different stored values and an attacker cannot use a precomputed table. A work factor makes each hash deliberately slow, so guessing costs the attacker time.

Java
PasswordEncoder bcrypt = new BCryptPasswordEncoder();
String a = bcrypt.encode("secret");
String b = bcrypt.encode("secret");
System.out.println(a.equals(b)); // false: a new random salt each time
System.out.println(bcrypt.matches("secret", a)); // true
System.out.println(bcrypt.matches("Secret", a)); // false

Notice that encoding the same password twice gives two different strings. That is the salt at work, and it is why we must always use matches() rather than comparing strings ourselves.

2How DelegatingPasswordEncoder Chooses an Encoder

Applications live for many years, and the recommended algorithm changes during that time. DelegatingPasswordEncoder solves this by storing the algorithm's name in front of every hash, in curly braces. It holds a map from id to encoder and one default id used for new passwords.

The usual way to get one is PasswordEncoderFactories.createDelegatingPasswordEncoder(). It registers encoders for bcrypt, pbkdf2, scrypt, argon2, noop and several old formats, and uses bcrypt for new passwords in the versions most of us run (check your version, since defaults can change in a major release).

Java
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
String stored = encoder.encode("secret");
System.out.println(stored); // {bcrypt}$2a$10$N9qo8uLOickgx2ZMRZoMye...
System.out.println(encoder.matches("secret", stored)); // true: prefix says bcrypt
System.out.println(encoder.matches("secret", "{noop}secret")); // true: prefix says plain text
System.out.println(encoder.upgradeEncoding("{noop}secret")); // true: not the default id
encoder.matches("secret", "$2a$10$N9qo8uLOickgx2ZMRZoMye...");
// IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
Step throughWhat matches(raw, stored) does
  1. Read the text between { and }for example bcrypt
  2. Is that id in the map?
    YesPick that encoderNoUse the default for matches, or throw
  3. Delegate matches(raw, rest of value)the hash after the prefix
  4. Is the id the current default?
    YesNothing more to doNoupgradeEncoding() returns true, so re-encode on login

4 steps in order. Play them, or step through them one at a time.

The last line is a common surprise after upgrading an old application. Hashes stored without a prefix have no id, so the encoder refuses them. We can either add the prefix to every stored value with a one-off update, or call setDefaultPasswordEncoderForMatches() with the encoder the old values used.

3Taking {bcrypt}$2a$10$... Apart

A stored value has several parts, each with a meaning. {bcrypt} is DelegatingPasswordEncoder's id. Everything after it is a standard bcrypt string. 2a is the bcrypt version marker. 10 is the cost factor, a power of two: bcrypt runs its expensive key setup 2 to the power 10, so 1,024 times. Ten is the default strength of BCryptPasswordEncoder. Then come 22 characters of salt and 31 characters of the hash itself.

Because the salt and the cost are inside the string, bcrypt needs nothing else to check a password later. And because the cost is stored per value, we can raise it for new passwords without breaking old ones: each value is checked with the cost it was created with. Each step up in cost doubles the time per hash, so cost 12 takes about four times as long as cost 10.

Parts of a stored bcrypt value
PartExampleMeaning
{bcrypt}{bcrypt}id that DelegatingPasswordEncoder uses to choose the encoder
$2a$$2a$bcrypt version marker
10$10cost factor: 2^10 = 1,024 rounds of key setup
salt22 charactersrandom per password, stored in the clear
hash31 charactersthe result that matches() recomputes and compares

4This Is What Makes Migration Possible

Since every value names its own algorithm, one table can hold several formats at the same time. Old rows can say {sha256} or {pbkdf2}, new rows say {bcrypt}, and login works for all of them. When a user logs in with an old format, upgradeEncoding() returns true, and Spring Security's DaoAuthenticationProvider can save a new hash through a UserDetailsPasswordService. Over time the old formats disappear without anyone resetting a password. We do not need a migration weekend; we need one bean and a little patience.

5Why NoOpPasswordEncoder and Plain SHA-256 Are Unsafe

NoOpPasswordEncoder stores the password as it is. Anyone who reads the table, a backup, or a log of a database query sees every password, and because people reuse passwords, those passwords open accounts on other sites too. It is deprecated and exists only for tests and demos.

SHA-256 looks safer, but it was designed to be fast. A single modern GPU computes billions of SHA-256 hashes per second. With a leaked table of unsalted SHA-256 hashes, an attacker hashes a list of common passwords once and matches millions of accounts in minutes; even with a salt, testing each user's likely passwords costs almost nothing. With bcrypt at cost 10, the same GPU manages only thousands to tens of thousands of guesses per second, a slowdown of roughly a hundred thousand times.

Java
// Rough guesses per second on one modern GPU (orders of magnitude, they vary by hardware)
// SHA-256, unsalted ~ 10,000,000,000
// bcrypt, cost 10 ~ 20,000
// argon2id, 19 MiB memory ~ a few thousand, and memory limits parallel guessing

So the right choice is an adaptive, salted function, one whose cost we can tune upward as hardware gets faster: bcrypt, scrypt, Argon2 (Argon2id is the recommended variant) or PBKDF2 with a high iteration count. We tune it so one check takes a noticeable but acceptable time on our servers, often somewhere around a tenth of a second to a quarter of a second.

6Follow-up questions

Should we expose the {id} prefix? Does it help an attacker?

It does not meaningfully help. An attacker who has the table can recognise a bcrypt value from its $2a$ marker anyway. Security comes from the work factor and the salt, not from hiding the algorithm.

Does a pepper add anything?

A pepper is a secret added to every password and kept outside the database, for example in a key vault. It helps if only the database leaks. It adds operational work, because losing or rotating the pepper affects every hash, so it is a choice to make deliberately, on top of a slow hash, never instead of one.

Why is bcrypt limited to 72 bytes?

bcrypt only uses the first 72 bytes of its input, so very long passwords are silently truncated. Recent Spring Security versions reject longer inputs rather than truncate them (check your version). Argon2 has no such limit.

7Conclusion

In this answer, we saw that DelegatingPasswordEncoder encodes new passwords with its default encoder and uses the {id} prefix of each stored value to choose the encoder for checking, which lets several formats live together during a migration. We took {bcrypt}$2a$10$... apart into id, version, cost, salt and hash, and saw why plain text and fast hashes like SHA-256 fall to GPU guessing, while bcrypt, scrypt, Argon2 and PBKDF2 are tuned to be slow on purpose.

What to say in the interviewAbout 80 seconds aloud

Open with

DelegatingPasswordEncoder is a password encoder that delegates to others. It keeps a map from an id to an encoder and a default id. When it encodes, it uses the default and writes the id in curly braces in front of the hash.

Then, if they want more

When it checks, it reads that prefix and asks the matching encoder. PasswordEncoderFactories.createDelegatingPasswordEncoder gives me one with bcrypt as the default in the versions I use.

So in {bcrypt}$2a$10$..., bcrypt is the encoder id, 2a is the bcrypt version, 10 is the cost factor, meaning 2 to the 10 rounds, which is the default strength, and then come the 22-character salt and the hash.

Because each row names its algorithm, I can have old and new formats in the same table, and with upgradeEncoding and a UserDetailsPasswordService the user's hash is upgraded on their next login. NoOpPasswordEncoder stores plain text, so any leak exposes reused passwords.

SHA-256 is designed to be fast; a GPU does billions per second, so a leaked table can be cracked in minutes. I would use an adaptive, salted function like bcrypt, Argon2id, scrypt or PBKDF2, tuned so one check takes a noticeable fraction of a second on our hardware.

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 "What does DelegatingPasswordEncoder do, what does a stored value such as...": https://scrivio-interview-library.netlify.app/spring-boot/authentication/5e35df8747/