BBackend CourseLeroy Labs
0/0
Search the course

Password storage with BCrypt, Argon2 & breach checks

Why hashing (not encryption), salts, work factors, BCrypt cost 10 vs 12, Argon2id memory hardness, password policies, history, lockout and Have-I-Been-Pwned checks.

● intermediateSpring-Boot-StoreHostel-Web-AppReport-Manager-2IR3D (Kontentt)SperoTek (in dev)

The concept

You never need to recover a password, only to verify one. So you store a one-way hash rather than an encrypted value. If the database leaks, attackers should get nothing usable.

Fast hashes (SHA-256, MD5) are wrong for passwords. A GPU computes billions per second, so every common password falls instantly. Password hashes are designed to be slow and salted:

  • Salt: random bytes stored with each hash. The same password gets a different hash for each user, which defeats precomputed rainbow tables.
  • Work factor: a tunable cost, so as hardware gets faster you raise the cost.
  • Memory hardness (Argon2, scrypt): each guess needs lots of RAM, which makes GPU and ASIC attacks expensive.
PasswordEncoder enc = new BCryptPasswordEncoder(12);
String hash = enc.encode("correct horse battery staple");
// $2a$12$N9qo8uLOickgx2ZMRZoMye...   <- algorithm $ cost $ salt+hash, all in one string
boolean ok = enc.matches(raw, hash);   // constant-time comparison inside

Each +1 on BCrypt’s cost doubles the time. Aim for roughly 100–300 ms per hash on your production hardware.

In your projects

Project Algorithm Notes
Store BCryptPasswordEncoder() Cost 10 (the default)
Hostel BCryptPasswordEncoder(12) 4× slower than 10, which is better
RM BCryptPasswordEncoder() passwordEncoder.matches in AuthService.login
IR3D BCrypt + @StrongPassword
SperoTek Argon2id Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8() Strongest
SperoTek (in dev)PasswordEncoderConfig · PasswordPolicy · HibpBreachedPasswordChecker

SperoTek layers several controls:

  • Argon2id, the OWASP first choice and the Password Hashing Competition winner.
  • PasswordPolicy: a 12-character minimum and a history of 5 (you can’t reuse your last 5 passwords).
  • Breach check: HibpBreachedPasswordChecker asks Have I Been Pwned whether the password appeared in a breach. It uses k-anonymity: only the first 5 hex characters of the SHA-1 are sent, and the response is matched locally, so the password never leaves the server.
  • Lockout: 5 failed attempts locks the account for 15 minutes, and a notification email (account-locked template) is sent.

Alternatives

Algorithm Status
Argon2id OWASP’s first choice
scrypt Good, memory-hard
BCrypt Good and battle-tested; truncates at 72 bytes
PBKDF2 Use with high iterations (e.g. 600k for SHA-256) if you need FIPS compliance
SHA-256 / MD5 (even salted) ❌ Never
Encryption (AES) ❌ Wrong tool, since a stolen key reveals everything

Migrating algorithms

Spring’s DelegatingPasswordEncoder stores {bcrypt}$2a$... or {argon2}... prefixes. You can verify old hashes and re-hash on successful login with the new algorithm, so users migrate transparently.

  • NIST 800-63B guidance. Length beats complexity rules. Don’t force periodic rotation; do block breached passwords. SperoTek follows this.
  • Pepper. A server-side secret mixed into the hash and stored outside the DB (in an HSM or env var).
  • Timing attacks. Always run a hash comparison, even for unknown users, so response time doesn’t reveal whether an account exists.
  • Credential stuffing. Attackers replay leaked username/password pairs. Defend with rate limits, lockout, MFA and breach checks.

Check yourself

1. Why not SHA-256 for passwords?
2. HIBP k-anonymity sends…
3. BCrypt cost 10 → 12 makes hashing…