Choosing the cost factor
| Cost | Iterations (2^cost) | Relative time | Use for |
|---|---|---|---|
| 10 | 1,024 | 1× | minimum (OWASP), very high login volume |
| 11 | 2,048 | 2× | high-traffic apps |
| 12 | 4,096 | 4× | common default in Laravel and many frameworks |
| 13 | 8,192 | 8× | admin accounts, low-volume apps |
| 14 | 16,384 | 16× | offline secrets, rarely-used logins |
Measure on your production hardware: pick the highest cost where one hash still takes well under a second (100–300 ms is typical). Raise it over the years; most libraries re-hash on login when the stored cost is lower than the configured one.
Hashing and verifying in your language
// Node.js: npm i bcrypt (native) or bcryptjs (pure JS) import bcrypt from "bcrypt"; const hash = await bcrypt.hash(password, 12); const ok = await bcrypt.compare(password, hash);
import bcrypt hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt(rounds=12)) ok = bcrypt.checkpw(password.encode(), hashed)
$hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]); $ok = password_verify($password, $hash);
$2a$, $2b$ and $2y$ (PHP) prefixes are interchangeable for verification in current libraries. When you put a hash into a .env file, wrap it in single quotes: the $ signs would otherwise be read as variable references by dotenv-expand and Docker Compose.
bcrypt vs Argon2id vs scrypt vs SHA-256
| Algorithm | Built for passwords | Memory-hard | Notes |
|---|---|---|---|
| Argon2id | yes | yes | OWASP's first choice for new systems |
| scrypt | yes | yes | good alternative, used by Node's built-in crypto.scrypt |
| bcrypt | yes | no | battle-tested since 1999, 72-byte input limit |
| PBKDF2 | yes | no | FIPS-approved; needs very high iteration counts |
| SHA-256 / MD5 | no | no | billions of guesses per second on a GPU; never for passwords |
The difference that matters for stolen databases is speed: a fast hash lets an attacker test huge password lists cheaply, a deliberately slow one does not. Keep the rest of your secrets out of reach too, see .env best practices.