SSOGEN / Learn / Java Password Hashing: Argon2id, bcrypt and PBKDF2 in Spring
Learn
Java Password Hashing: Argon2id, bcrypt and PBKDF2 in Spring
Java password hashing stores a salted, deliberately slow hash instead of the password. In Spring Security, use Argon2id behind DelegatingPasswordEncoder, so old hashes keep working and upgrade at login.

Use Argon2id through Spring Security’s DelegatingPasswordEncoder. The encoder writes the algorithm id, the cost settings and a random salt into one string. One database column then holds everything needed to verify a password, and you can raise the cost later without changing the schema. Argon2 needs Bouncy Castle on the classpath.
This is a real hash from Spring Security 7.1.2, made with the Argon2id settings used through this page:
{argon2id}$argon2id$v=19$m=19456,t=2,p=1$O0pjN06hf7CkLQmxWvR92g$0ZlVqaFih0otRq5r9U10YBHWX79fCnNtciiihRr596o- Id.
{argon2id}tellsDelegatingPasswordEncoderwhich encoder verifies this value. The id is your own label. - Variant and version.
$argon2id$v=19names the algorithm and its format version. - Cost.
m=19456,t=2,p=1is 19,456 KiB of memory, 2 passes and parallelism 1. - Salt.
O0pjN06hf7CkLQmxWvR92gis 16 random bytes in base64, unique to this password. - Hash.
0ZlVqaFih0otRq5r9U10YBHWX79fCnNtciiihRr596ois the 32-byte result.
The legal range of every field, and what each cost setting means for your server, is in Argon2 hash, decoded field by field.
Which algorithm should a Java app use?
OWASP’s Password Storage Cheat Sheet orders the algorithms by situation. Each row gives the OWASP minimum and the Spring class.
- Argon2id. The first choice. At least 19 MiB of memory, 2 iterations and parallelism 1. Spring class:
Argon2PasswordEncoder. - scrypt. When Argon2id is not available. N=2^17, r=8, p=1. Spring class:
SCryptPasswordEncoder. - bcrypt. Legacy systems. Work factor 10 or more, and a password limit of 72 bytes. Spring class:
BCryptPasswordEncoder. - PBKDF2. When FIPS-140 is required. HMAC-SHA-256 with 600,000 iterations, or HMAC-SHA-512 with 220,000. Spring class:
Pbkdf2PasswordEncoder.
What does each setting cost on a real JVM?
We timed each OWASP setting through Spring’s own encoder classes. Argon2id spends memory where bcrypt and PBKDF2 spend only time, so the milliseconds alone do not rank the algorithms.
| Algorithm | Parameters | Median encode time |
|---|---|---|
| bcrypt | cost 10 (OWASP minimum, legacy) | 88 ms |
| bcrypt | cost 12 | 353 ms |
| bcrypt | cost 14 | 1,400 ms |
| Argon2id | m=19456 (19 MiB), t=2, p=1 (OWASP minimum) | 41 ms |
| Argon2id | m=47104 (46 MiB), t=1, p=1 (OWASP) | 62 ms |
| Argon2id | m=12288 (12 MiB), t=3, p=1 (OWASP) | 33 ms |
| Argon2id | Spring defaultsForSpringSecurity_v5_8: m=16384, t=2, p=1 |
31 ms |
| scrypt | N=131072 (2^17), r=8, p=1 (OWASP) | 558 ms |
| scrypt | Spring defaultsForSpringSecurity_v5_8: N=32768, r=8, p=1 |
275 ms |
| PBKDF2-HMAC-SHA256 | 600,000 iterations (OWASP, FIPS-140) | 798 ms |
| PBKDF2-HMAC-SHA512 | 220,000 iterations (OWASP) | 336 ms |
The run used OpenJDK 21.0.12.1 on one thread of a 2.2 GHz server CPU, Spring Security crypto 7.1.2 and Bouncy Castle bcprov-jdk18on 1.87 for Argon2. The password had 28 characters. Each row is the median of 15 encode calls after 3 warm-up calls. A second run on the same machine differed by up to 16 percent. Timings vary with hardware; measure on your own servers.
Spring’s reference suggests tuning to about one second, and OWASP says a hash should take less than one second.
Java bcrypt, Argon2 and PBKDF2 sample code
Each algorithm is one constructor and two methods, encode and matches. Add org.springframework.security:spring-security-crypto 7.1.2, plus org.bouncycastle:bcprov-jdk18on for Argon2. Outside Spring Boot, also add commons-logging. Imports are omitted.
PasswordEncoder encoder = new BCryptPasswordEncoder(12);String hash = encoder.encode(rawPassword); // $2a$12$...boolean ok = encoder.matches(rawPassword, hash);// salt 16 bytes, hash 32 bytes, parallelism 1, memory in KiB, iterationsPasswordEncoder encoder = new Argon2PasswordEncoder(16, 32, 1, 19456, 2);String hash = encoder.encode(rawPassword); // $argon2id$v=19$m=19456,t=2,p=1$...// CPU cost N, block size r, parallelism p, key length, salt lengthPasswordEncoder encoder = new SCryptPasswordEncoder(131072, 8, 1, 32, 16);String hash = encoder.encode(rawPassword); // $110801$... (0x11 = 17 = log2 N, then r=8, p=1)PasswordEncoder encoder = new Pbkdf2PasswordEncoder("", 16, 600000, Pbkdf2PasswordEncoder.SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA256);String hash = encoder.encode(rawPassword); // 96 hex characters: salt and hash, no costThe PBKDF2 value carries no iteration count, so record it in the {id} prefix described next. Pbkdf2PasswordEncoder is in org.springframework.security.crypto.password in the 5.8.16, 6.5.5 and 7.1.2 jars alike, so older snippets keep their import.
Upgrading old hashes with DelegatingPasswordEncoder
The {id} prefix lets old and new hashes sit in the same column. NIST SP 800-63B-4 asks verifiers to store a reference to the scheme and its cost factor with each password, so that you can migrate later.
- Register every algorithm already stored, each under an id, and name the id for new passwords.
- Give old rows their prefix, for example
UPDATE users SET password = '{bcrypt}' || password. A value with no prefix and no default encoder throwsIllegalArgumentException. - At each successful login, call
upgradeEncoding. When it returns true, encode the password again and save it.
Map<String, PasswordEncoder> encoders = new HashMap<>();encoders.put("argon2id", new Argon2PasswordEncoder(16, 32, 1, 19456, 2));encoders.put("bcrypt", new BCryptPasswordEncoder(10));DelegatingPasswordEncoder encoder = new DelegatingPasswordEncoder("argon2id", encoders);
if (encoder.matches(rawPassword, stored) && encoder.upgradeEncoding(stored)) { user.setPassword(encoder.encode(rawPassword)); // {argon2id}$argon2id$v=19$m=19456,t=2,p=1$... users.save(user);}This is the session we ran on Spring Security 7.1.2:
Before login: the column holds {bcrypt}$2a$10$.... matches returns true and upgradeEncoding returns true.
After login: the column holds {argon2id}$argon2id$v=19$m=19456,t=2,p=1$.... upgradeEncoding returns false, and the bcrypt hash is gone.
Accounts that never sign in keep the old hash. OWASP offers two answers: expire and delete hashes of long-inactive users, or wrap the old hash in the new algorithm, as in bcrypt(md5(password)), and replace it with a direct hash at the next login.
Limits that bite: 72 bytes, salts and peppers
- bcrypt length.When a password is longer than 72 bytes, then
BCryptPasswordEncoder.encodethrowsIllegalArgumentException, whilematchesignores the bytes past 72. In our run a 73-character ASCII password matched the hash of its first 72 characters. - Salt.When you hash, then let the encoder generate the salt. It is stored inside the hash string, and NIST SP 800-63B-4 requires at least 32 bits.
- Pepper.When you add a pepper, then keep it in a secrets vault or HSM, never in the database. Changing a pepper forces every affected user to reset.
NIST also says verifiers should permit passwords of at least 64 characters. Outside ASCII, 64 characters can exceed 72 bytes in UTF-8, so check your bcrypt limit against your users’ alphabets. What a service must enforce is covered in NIST password guidelines and password policy; to measure how strong a chosen password is, use the password entropy calculator.
Moving existing users into SSOGEN
The SSOGEN users CSV import has an optional password_hash column that takes the hash your old system already stored, never the password. Recognized forms are bcrypt ($2a$, $2b$, $2y$), {SSHA512}, {SSHA256}, and PBKDF2 in Django’s and passlib’s layouts, each also as SHA-512. A person with a hash keeps signing in with the password they know and is not sent an invitation.
The steps are in add many people from a file. To take sign-in out of a Tomcat or Spring Boot application, see the SSOGEN Java agent.
Questions Java developers ask
Which hashing algorithm should I use for passwords in Java?
Argon2id. OWASP’s Password Storage Cheat Sheet puts it first, with at least 19 MiB of memory, 2 iterations and parallelism 1. Use scrypt where Argon2id is not available, bcrypt at work factor 10 or more for legacy systems, and PBKDF2 with 600,000 HMAC-SHA-256 iterations when FIPS-140 is required.
How do I hash a password with bcrypt in Java?
Create a BCryptPasswordEncoder from spring-security-crypto with a cost of 10 or more. Call encode to get a 60-character hash that carries its own salt and cost, and call matches(raw, hash) at login. Cost 12 took 353 ms per hash in our run, and passwords are limited to 72 bytes.
Does Spring Security support Argon2?
Yes. Argon2PasswordEncoder ships in spring-security-crypto, and Spring’s reference states that it requires Bouncy Castle, so add bcprov-jdk18on. The defaultsForSpringSecurity_v5_8 settings use 16 MiB, below OWASP’s 19 MiB minimum, so pass your own parameters. We ran it on Spring Security 7.1.2 with bcprov 1.87.
How do I upgrade existing password hashes to a stronger algorithm?
Store them with DelegatingPasswordEncoder, whose {id} prefix names the algorithm. At each successful login, call upgradeEncoding on the stored value; when it returns true, encode the password you just verified and save it. Accounts that never sign in keep the old hash until you expire them.
Is SHA-256 enough for hashing passwords in Java?
No. SHA-256 is a fast general-purpose digest with no work factor, so an attacker holding stolen hashes can test guesses at very high speed. OWASP lists fast algorithms such as SHA-256 as unsuitable for password storage. Use Argon2id, scrypt, bcrypt or PBKDF2, which are built to be slow and take a cost setting.
Timings and behaviours above are from our own run on 2026-11-17. More Java and identity explainers are on the Learn hub.
Sources, all read 2026-11-17:
- OWASP Password Storage Cheat Sheet
- NIST SP 800-63B-4, section 3.1.1.2
- Spring Security 7.1.2, Password Storage
- Maven Central: spring-security-crypto (7.1.2, 6.5.5 and 5.8.16 jars) and bcprov-jdk18on (1.87)
Updated