Stage 3 · Build
Authentication & Authorization
Password Hashing
Store passwords with bcrypt or Argon2id, per-user salts, cost factors, and constant-time comparison.
Why Hash Passwords
Never store passwords in plaintext. If your database is compromised, every user account is exposed. Hashing transforms passwords into irreversible strings. Even if attackers get the hashes, they cannot recover the original passwords without brute force.
Encryption is reversible with a key. Hashing is one-way. You verify a password by hashing the input and comparing it to the stored hash. You never decrypt the hash back to the password.
bcrypt
bcrypt is the standard password hashing algorithm. It includes a per-password salt, is memory-hard to resist GPU attacks, and has a configurable cost factor. Go's crypto/x509 package includes bcrypt support.
Argon2id
Argon2id is the winner of the Password Hashing Competition. It is memory-hard and resistant to both GPU and side-channel attacks. It is the recommended algorithm for new applications.
Cost Factors
The cost factor determines how slow the hash computation is. Higher cost means slower hashing, which makes brute force attacks impractical. Balance security with response time.
| Algorithm | Recommended Parameters | Time per Hash |
|---|---|---|
| bcrypt | Cost 12 | ~250ms |
| Argon2id | m=65536, t=1, p=4 | ~100ms |
| Argon2id (high security) | m=256000, t=3, p=4 | ~500ms |
Run a benchmark to find the right cost factor. Hash 1000 passwords and aim for 100-300ms per hash. This is slow enough to resist brute force but fast enough to not impact login response time.
Constant-Time Comparison
Regular string comparison short-circuits on the first differing byte. This leaks timing information that attackers can use to guess the hash one character at a time. Constant-time comparison takes the same time regardless of where strings differ.
Password Validation
Enforce password requirements at signup and password change. Minimum length is the most important rule. Complexity requirements often hurt more than they help.
Some systems truncate passwords at a maximum length. This silently reduces security. Set a generous maximum (128 characters) and hash the full input.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.