Learning objectives
After reading this article you will be able to:
- Compare Argon2id, scrypt, bcrypt and PBKDF2 on the criteria that matter
- Set the OWASP minimum parameters in Node.js and Python without running out of RAM
- Migrate passwords stored with MD5, SHA-1 or bcrypt to Argon2id
On this page
- Why is a regular hash not enough for passwords?
- How do Argon2id, scrypt, bcrypt and PBKDF2 compare?
- How do you use Argon2id in Node.js and Python?
- How much memory does Argon2 use on the server?
- What about bcrypt's 72-byte limit?
- How do you migrate from MD5, SHA-1 or bcrypt to Argon2id?
- Which one should you pick for each scenario?
- How to do this on Square Cloud
- FAQs
Every system with a login stores something that lets it check the user's password. What decides the damage of a breach is what exactly was stored: the plain password, a fast hash such as MD5, or a slow, salted hash designed to resist attacks.
Storing a password is not encrypting it: you never need the password back, only to check whether the typed one matches. The difference is covered in hashing vs encryption vs encoding.
Why is a regular hash not enough for passwords?#
SHA-256, SHA-1 and MD5 were built to be fast. That is great for checking a file and terrible for passwords: anyone holding the hash can test huge lists of likely passwords on a GPU, with no attempt limit. This is the offline attack, the unthrottled version of a brute force attack.
A password hashing function changes the game in three ways:
- It is slow on purpose. A cost parameter sets the work per hash. A fraction of a second does not bother a login, but it multiplies the cost of testing millions of guesses.
- It uses a salt. A random value per password turns identical passwords into different hashes and defeats precomputed tables. NIST, in SP 800-63B, requires a salt of at least 32 bits; RFC 9106 considers 128 bits enough for Argon2.
- It can require memory. Argon2 and scrypt are memory-hard: each guess needs a block of RAM, which limits how many guesses a GPU runs in parallel.
How do Argon2id, scrypt, bcrypt and PBKDF2 compare?#
Four criteria drive the choice:
- GPU resistance. The more memory per hash, the more expensive it is to parallelize the attack. PBKDF2 relies only on CPU iterations.
- Memory on your server. The memory that slows the attacker also takes your app's RAM on every login.
- Support in your stack. A maintained library that generates the salt and stores the parameters with the hash prevents most mistakes.
- Compliance. If the system must follow FIPS-140, OWASP points to PBKDF2.
OWASP's order, in the Password Storage Cheat Sheet, is Argon2id; scrypt when Argon2id is not available; bcrypt only for legacy systems; PBKDF2 when FIPS-140 is required. The table shows the minimums it recommends today. These values change over time: check the page before you pin yours:
| Argon2id | scrypt | bcrypt | PBKDF2 | |
|---|---|---|---|---|
| OWASP minimum | m=19 MiB, t=2, p=1 | N=2^17, r=8, p=1 | cost 10 | 600,000 iterations with HMAC-SHA256 |
| Memory per hash | configurable | 128 MiB at that minimum | small and fixed | almost none |
| GPU resistance | high (needs memory) | high (needs memory) | moderate | low (CPU only) |
| Input limit | none in practice | none in practice | 72 bytes | none in practice |
OWASP also lists Argon2id settings that trade memory for iterations with equivalent defense: 46 MiB with t=1, 19 MiB with t=2, 12 MiB with t=3, 9 MiB with t=4 and 7 MiB with t=5, all with p=1. RFC 9106, which standardizes Argon2, makes Argon2id the mandatory variant and tells you to pick it when in doubt.
scrypt ships with Node.js (crypto.scrypt) and Python (hashlib.scrypt), but both reject the OWASP minimum until you raise maxmem, which defaults to 32 MiB.
How do you use Argon2id in Node.js and Python?#
In Node.js, the argon2 package uses Argon2id by default, generates the salt and returns a PHC-format string that keeps algorithm, parameters, salt and hash in a single column. The example pins the OWASP minimum and rehashes when the parameters change:
// npm install argon2
import * as argon2 from "argon2";
// Current OWASP minimum for Argon2id (check the cheat sheet): 19 MiB, t=2, p=1
const OPTIONS = {
type: argon2.argon2id,
memoryCost: 19456, // in KiB
timeCost: 2,
parallelism: 1,
};
export async function hashPassword(password) {
// The random salt and the parameters go inside the returned string
return argon2.hash(password, OPTIONS);
}
export async function checkPassword(storedHash, password) {
if (!(await argon2.verify(storedHash, password))) {
return { ok: false, newHash: null };
}
// Hash created with old parameters? Redo it now, while you have the password
const newHash = argon2.needsRehash(storedHash, OPTIONS)
? await hashPassword(password)
: null;
return { ok: true, newHash };
}The hash starts with $argon2id$v=19$m=19456,t=2,p=1$. When newHash comes back filled, store it in place of the old one. Recent Node.js versions have a built-in crypto.argon2(), but it returns raw bytes only: the salt and the storage format are up to you.
In Python, argon2-cffi does the same job:
# pip install argon2-cffi
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
# Current OWASP minimum for Argon2id (check the cheat sheet): 19 MiB, t=2, p=1
ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
def hash_password(password: str) -> str:
return ph.hash(password)
def check_password(stored_hash: str, password: str) -> tuple[bool, str | None]:
try:
ph.verify(stored_hash, password)
except VerifyMismatchError:
return False, None
# Parameters changed since the hash was created? Make a new one now
new = ph.hash(password) if ph.check_needs_rehash(stored_hash) else None
return True, newWithout parameters, both libraries use RFC 9106's second recommended profile: 64 MiB, 3 iterations and a parallelism of 4. It is stronger than the OWASP minimum and heavier on RAM.
How much memory does Argon2 use on the server?#
Every hash in progress holds the full m. The math: peak memory ≈ m × concurrent hashes. With the 64 MiB default, eight logins at the same time ask for 512 MiB just for Argon2, enough to take down a small container with an out of memory error.
In Node.js, the argon2 package computes on the libuv threadpool, which has 4 threads by default (UV_THREADPOOL_SIZE). So one process runs at most 4 hashes at once: a 76 MiB peak at 19 MiB, 256 MiB at 64 MiB. A bigger threadpool means a bigger peak. In Python, count per worker and per thread: Gunicorn with 4 sync workers runs up to 4 hashes at once.
Three adjustments cover almost every case:
- Pick the profile by RAM. With little memory, use an equivalent OWASP setting, such as 12 MiB with t=3 or 9 MiB with t=4.
- Cap concurrency. A queue or a semaphore on the login route keeps the peak predictable.
- Limit attempts. Rate limiting per IP and per account stops anyone from using your login to burn CPU and RAM; OWASP warns that an excessive cost opens the door to denial of service.
Measure on the real server: sizing RAM and CPU helps fit that peak alongside the rest of the app.
What about bcrypt's 72-byte limit?#
bcrypt uses only the first 72 bytes of the password. Bytes, not characters: in UTF-8, "é" takes 2 bytes and an emoji takes 4. NIST recommends a maximum length of at least 64 characters and requires verifying the entire password, without truncation; 64 emoji add up to 256 bytes.
The npm bcrypt package silently ignores the excess, so two passwords sharing their first 72 bytes match each other. Python's bcrypt package, since version 5.0, rejects the input with a ValueError. If you keep bcrypt, check the length in bytes:
// npm install bcrypt
import bcrypt from "bcrypt";
const LIMIT = 72; // bcrypt only reads the first 72 bytes
export async function hashWithBcrypt(password) {
if (Buffer.byteLength(password, "utf8") > LIMIT) {
throw new Error("Password over 72 bytes: bcrypt would ignore the rest");
}
return bcrypt.hash(password, 12); // cost 12; OWASP asks for at least 10
}Pre-hashing (SHA-256 before bcrypt) gets around the limit, but OWASP calls it dangerous: the binary output can contain null bytes, and a fast hash of the same password leaked elsewhere enables password shucking. If you must, it suggests HMAC with a pepper and base64.
How do you migrate from MD5, SHA-1 or bcrypt to Argon2id?#
You only have the hashes. OWASP describes two paths, and they combine.
Rehash on login. If the current hash is still acceptable (bcrypt, for example), verify with the old algorithm and, when the password matches, store an Argon2id hash in its place. The prefix tells you which algorithm to use: $2a$, $2b$ or $2y$ is bcrypt, $argon2id$ is Argon2id. In Django, put Argon2PasswordHasher at the top of PASSWORD_HASHERS (with django[argon2] installed) and it rehashes each password on login. Users who never come back stay on the old hash; OWASP suggests expiring the hashes of long-inactive accounts and requiring a reset.
Wrap now. For MD5 or SHA-1, do not wait: apply Argon2id on top of the old hash for every row at once, without needing the password. On login, compute the MD5 of the typed password, verify the wrapper and replace it with a direct Argon2id hash:
import hashlib
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
PREFIX = "md5+" # marks wrapped hashes
def wrap_md5(md5_hex: str) -> str:
# Runs once over every old row, no password needed
return PREFIX + ph.hash(md5_hex)
def verify(stored_hash: str, password: str) -> tuple[bool, str | None]:
legacy = stored_hash.startswith(PREFIX)
candidate = hashlib.md5(password.encode()).hexdigest() if legacy else password
try:
ph.verify(stored_hash.removeprefix(PREFIX), candidate)
except VerifyMismatchError:
return False, None
if legacy or ph.check_needs_rehash(stored_hash):
return True, ph.hash(password) # store this in place of the old hash
return True, NoneOWASP treats wrapping as a transition, because layered hashes can be easier to crack. When the migration ends, only direct Argon2id hashes are left.
An optional extra is a pepper: an app-wide secret combined with every password and kept outside the database. It only helps when it lives away from the hashes, and it cannot be changed without each user's password. It belongs in an environment variable or a vault, never in the code; the options are in where to store secrets.
Which one should you pick for each scenario?#
| Scenario | Pick |
|---|---|
| New project | Argon2id, at the OWASP minimum or the library default if RAM allows |
| Little RAM or many concurrent logins | Argon2id with 9 MiB and t=4, plus capped concurrency |
| No Argon2 library on the platform | scrypt with N=2^17, r=8, p=1 and a raised maxmem |
| Existing system on bcrypt | cost 10 or more, a 72-byte check and migration on login |
| Database with MD5 or SHA-1 | wrap in Argon2id now and replace on login |
| FIPS-140 requirement | PBKDF2-HMAC-SHA256 at the current OWASP minimum (600,000 iterations) |
How to do this on Square Cloud#
The hashes live in your database, and Square Cloud offers PostgreSQL, MySQL, MongoDB and Redis hosting from the Standard plan. An Argon2id string fits in a regular text column. The connection URL the dashboard shows when the database is created goes into an environment variable of your app; a pepper, if you use one, goes into another, away from the users table.
Argon2's memory comes out of your app's RAM. The plan's RAM is a pool shared by your apps, with a minimum of 512 MB per web app, and you change each app's RAM in its settings. Do the math from the memory section before fixing the parameters: on Square Cloud, an app that runs out of memory stops and does not come back on its own, so you raise the RAM and start it again. Real-time RAM metrics show the peak during a login load test.
Databases get automatic daily snapshots, and each snapshot carries the users table with it: one more reason for it to hold only strong hashes. See the options on Databases.
Infrastructure
L3/L4/L7 DDoS mitigation and a WAF on every plan
FAQs
Can I use SHA-256 to store passwords?
Not on its own. SHA-256 is built to be fast, and speed is exactly what helps whoever leaked your database test passwords in bulk. For passwords, use a slow, salted function such as Argon2id, scrypt, bcrypt or PBKDF2 with many iterations.
Is bcrypt still secure?
With a cost of 10 or more (the minimum OWASP asks for today), yes, but OWASP recommends it only for legacy systems where Argon2id and scrypt are not available. It also uses only the first 72 bytes of the password. For a new project, prefer Argon2id.
Do I need to generate and store the salt separately?
No, if you use a library such as argon2 (Node.js), argon2-cffi or bcrypt (Python). They generate a random salt for every hash and write it into the resulting string, along with the algorithm and the parameters.
How long should a password hash take?
OWASP gives less than one second per hash as a general rule and says there is no universal ideal cost. Measure on your production server, with the login load you expect.
More in Security
See all 24 articles- Authentication vs authorization: the difference in practiceAuthentication confirms who you are; authorization decides what you can do. Learn 401 vs 403, roles, scopes and the bug of checking only the login, with code.
- JWT vs cookie sessions: which to use for loginWith sessions, state lives on the server and the cookie holds an ID; with JWT, the data travels in the token. Compare logout, CSRF, XSS and multiple instances.
- What is SQL injection and how to prevent itSQL injection happens when user input becomes part of a SQL query. See how the attack works and how to prevent it with parameterized queries in Node.js and Python.
- What is a brute force attack and how to defend against itA brute force attack tests passwords in bulk until one works. Learn the types (spraying, credential stuffing), how they work and the defenses, with code that runs.