How Should an App Store Passwords in a Database?
An app should never store the password itself. Instead it stores a password hash: the result of running the password through a slow, one-way function designed for passwords, such as Argon2id, scrypt or bcrypt, with a random salt added for each user. When someone logs in, the app hashes what they typed in the same way and checks whether the result matches what is stored. If the database is ever leaked, attackers get hashes rather than passwords, and a good password hashing function makes guessing the original passwords slow and expensive. In practice you do not write any of this yourself: you use a well-known library for your language, which handles the salt and the settings for you.
Why not just save the password?
In our SQL injection hands-on lesson, the toy login table keeps passwords as plain text so the example stays short. Real apps must not, for three reasons:
- Leaks happen. Backups get copied, laptops get lost and bugs expose data. A table of plain passwords is a disaster in all of those cases.
- People reuse passwords. A password leaked from your small club app might also open someone's email.
- You do not need it. To check a login, the app only needs to know whether the typed password is correct, not what the password is.
Three words to know: hash, salt, slow
| Word | Plain meaning | Why it matters |
|---|---|---|
| Hash | A function that turns any text into a fixed-looking string; you cannot run it backwards | The database stores the hash, not the password |
| Salt | A random value made for each user and mixed into the hash | Two users with the same password get different hashes, so one guess cannot crack many accounts at once |
| Slow on purpose | Password hashing functions take noticeable time and memory for each guess | Logging in once is quick enough, but trying billions of guesses becomes very expensive |
That last row is why general-purpose hashes such as SHA-256 on their own are not the right tool: they are designed to be fast, which helps an attacker. The OWASP Password Storage Cheat Sheet, a widely used free guide, recommends Argon2id first, with scrypt, bcrypt and PBKDF2 as alternatives in particular situations.
What the table looks like
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL
);
There is no password column, only password_hash. Most libraries produce one string that already contains the algorithm name, its settings and the salt, so you do not need separate columns for them. Use a text column long enough for the whole string; the library's documentation says how long it can be. The UNIQUE rule on email is one of the safety rails described in constraints as safety rails.
A tiny example in Python
This uses the argon2-cffi package, one common choice. Other languages have equivalent libraries, and your web framework may already include one.
from argon2 import PasswordHasher
ph = PasswordHasher()
# sign-up: store only the hash
password_hash = ph.hash(typed_password)
cur.execute("INSERT INTO users (email, password_hash) VALUES (?, ?)",
(email, password_hash))
# login: fetch the hash and let the library check it
row = cur.execute("SELECT password_hash FROM users WHERE email = ?",
(email,)).fetchone()
try:
ph.verify(row[0], typed_password)
print("Logged in")
except Exception:
print("Wrong email or password")
Two things to notice. The queries use ? placeholders, the same habit that stops SQL injection; see how to use placeholders for the Node.js version. And the app never compares passwords itself: verify does the careful comparison. A real app would also handle a missing user without crashing and show the same message for "no such email" and "wrong password".
Mistakes beginners make
- Encrypting instead of hashing. Encryption can be reversed with a key. If the key leaks with the database, so do the passwords.
- Using a fast hash alone. MD5, SHA-1 or a single round of SHA-256 is far too quick to guess against.
- Writing your own scheme. Home-made salting and hashing almost always has a flaw. Use the library.
- Logging passwords. Debug logs that print the request body can capture passwords in plain text.
- Emailing passwords. Password reset should send a one-time link, never the password.
- Keeping secrets in code. Database credentials belong in environment variables, as in how to keep a connection string in an env variable.
Where to read more
For trusted guidance, start with the OWASP Password Storage Cheat Sheet and the authentication guidance in NIST Special Publication 800-63B from the US National Institute of Standards and Technology. Our directory of trusted sources on SQL injection lists more security organisations worth knowing. For a club or class project, using your framework's built-in login system, or a well-known authentication service, is often the safest choice of all.
Frequently asked questions
Can I get a user's password back from the hash?
No, and that is the point. Hashing is one-way. If a user forgets their password, the app lets them set a new one through a reset link.
What is a salt?
A random value created for each user and mixed into the hash, so identical passwords produce different hashes. Good libraries create and store it for you.
Which algorithm should a beginner use?
Use a maintained library that implements Argon2id or bcrypt, following the OWASP Password Storage Cheat Sheet, and keep the library's default settings unless you have a reason to change them.
Continue with more lessons from the beginner hub.