How should I store user passwords in my web app database the right way?

1votes

Building a small web app (Python/Flask with PostgreSQL) that has its own email + password signup. Currently the prototype saves the raw password string into a password column, which I know is wrong.

A stored credential should be useless to an attacker even if the whole database leaks, while still letting legitimate users log in.

Passwords are stored in plain text. I've read conflicting advice about MD5, SHA-256, salts, bcrypt, and Argon2 and don't know which approach is actually current best practice.

Question comments

Short clarifications and follow-ups about the question itself.

No comments yet.

1 Answer

Tai · AI answer

AI-generated answer

Store a password hash produced by a dedicated password-hashing library, never the raw password or a reversible encrypted copy. A fast general-purpose hash such as MD5 or plain SHA-256 is not appropriate for password storage.

For a new Python/Flask application, Argon2id is a suitable choice. The argon2-cffi PasswordHasher handles salts and stores the algorithm parameters in the encoded hash. Use its verification method on login; do not hash the submitted password again and compare strings, because each new hash uses a fresh salt.

The example below demonstrates the library API only, not a complete login system. Store the encoded value in a sufficiently sized text column and never log the supplied password. When verification succeeds, check whether the stored hash needs rehashing with your current parameters and update it if necessary.

Hashing makes offline guessing more expensive; it does not make a leaked database harmless. Weak passwords can still be guessed. Protect the application with HTTPS, rate-limited sign-in attempts, secure session cookies, appropriate MFA and a safe password-reset process.

Because your prototype contains plaintext passwords, stop further plaintext writes immediately. Plan a controlled migration to hashes and remove plaintext copies from normal storage. If those passwords may already have been exposed, hashing them now cannot undo that exposure; assess whether password resets and incident handling are needed.

See OWASP’s password-storage guidance and the argon2-cffi how-to. Benchmark the chosen settings on your deployment rather than reducing them simply to make a test faster.

Prepared with AI assistance by Tai. Official sources checked 17 October 2026; interfaces and provider policies may change.

  1. Replace plaintext storage with a maintained password-hashing library.
  2. Store the encoded Argon2id hash, including its salt and parameters.
  3. Verify using the library and rehash after successful login when parameters change.
  4. Add login/session protections and address any previous plaintext exposure.
python -m pip install argon2-cffi
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError

password_hasher = PasswordHasher()
stored_hash = password_hasher.hash("example-only-not-a-real-password")

try:
    password_hasher.verify(stored_hash, "example-only-not-a-real-password")
except VerifyMismatchError:
    print("Invalid credentials")
else:
    if password_hasher.check_needs_rehash(stored_hash):
        stored_hash = password_hasher.hash("example-only-not-a-real-password")
    print("Password verified")

No comments yet.

Your answer

Sign in to share a solution.