Skip to content

Password hashing and salting

Introduction

Turn a password into gibberish with a dedicated algorithm that only works one way and cannot be reversed, then store it; mix in a random "salt" per user so identical passwords produce different results.

Why use it

  • Plaintext storage: a database leak exposes everything, and internal staff can see it too.
  • MD5 / SHA-1 / a single SHA-256: these algorithms are designed to be “fast”; an attacker can try billions of guesses per second, and common passwords fall in minutes.
  • No salt: identical passwords produce identical hashes, so an attacker can look them up in a precomputed “rainbow table” (a large dictionary of common passwords to hashes) in one pass.

Key concepts

  • Hash: a one-way transform; the same input always yields the same output, and it cannot be reversed.
  • Salt: a random value generated per user, mixed into the hash computation, and stored with the result (the salt is not a secret).
  • Slow hashing: deliberately designed to be “slow” (Argon2 and scrypt also consume memory) so guessing passwords becomes too expensive.

How to do it

  1. Choose an algorithm designed for passwords: Argon2id (preferred), bcrypt, scrypt, or PBKDF2.
  2. Use the library’s default parameters; the salt is generated and encoded into the result string for you—do not assemble salts yourself.
  3. Verify with the library’s verify function; do not compare strings yourself.
  4. Parameters will be raised over time, so leave enough room in the storage field and support “on successful login, rehash if the parameters are outdated”.
  5. Combine with rate limiting against brute force; see Rate limiting. For the overall auth flow, see Authentication and authorization.

Component comparison

Argon2id

Self-hosted open sourceOpen sourceOpsLow

Currently the most recommended password hashing algorithm: deliberately both slow and memory-hungry, making bulk password guessing on GPUs too costly to be worthwhile.

Best forNew projects that manage user passwords themselves and want the most recommended algorithm from the start.

Avoid whenServers with very little memory or legacy environments where the library is hard to install; using ASP.NET Identity without wanting to replace the hasher yourself.

Pros

Consumes both time and memory, which resists GPU and dedicated-hardware cracking well

The preferred recommended algorithm for new projects

Parameters (memory, iterations, parallelism) are tunable and can be raised as hardware improves

Cons

You must evaluate the parameters yourself (too high slows logins and exhausts memory; too low is insecure)

.NET has no built-in implementation, so a third-party library is needed

Many concurrent verifications at login peaks use a lot of memory

Official docs ↗

bcrypt

Self-hosted open sourceOpen sourceOpsLow

A veteran password hashing algorithm in use for over twenty years, with mature libraries in every language, secure enough and simple to use.

Best forProjects that want something mature and safe with easy-to-find libraries and simple configuration, or legacy systems with existing bcrypt data.

Avoid whenNew projects where Argon2id is available; when very long passphrases must be supported.

Pros

Long history and battle-tested, with ready-made libraries in nearly every language and framework

Only one "cost" parameter, hard to misconfigure

Low memory use, friendly to low-spec servers

Cons

Does not use memory, so it resists GPU cracking less well than Argon2id

Traditional implementations only process the first 72 bytes, ignoring the rest of very long passwords

Not the first choice for new projects today

Official docs ↗

Built into the framework (ASP.NET Identity PasswordHasher / Django default PBKDF2)

Built into the frameworkOpsLow

Use the framework's own password handling directly, with no algorithm to pick and no code to write; the framework has already chosen sensible defaults.

Best forProjects already using ASP.NET Core Identity or Django's built-in user system that want less to worry about.

Avoid whenYou have higher security requirements and want Argon2id without being limited by framework defaults (Django can switch via settings; Identity needs a custom hasher).

Pros

No extra dependency: once the framework is installed you can use it directly

Salt, format, and verification are handled by the framework, so mistakes are rare

An upgrade often raises the strength, and an old hash can be upgraded on login

Cons

PBKDF2 spends only time, so it resists GPUs less well than Argon2id

The default iteration count may be out of date, so check it or raise it yourself

Changing the algorithm means extending the framework hasher or changing its config

scrypt

Self-hosted open sourceOpen sourceOpsLow

A memory-hard hashing algorithm with a similar idea to Argon2; more GPU-resistant than bcrypt, but less popular with fewer library choices.

Best forPlatforms that already have ready-made scrypt support (such as Python's standard library hashlib), or compatibility with existing scrypt data.

Avoid whenNew projects with no special reason.

Pros

It also spends memory, so it resists dedicated cracking hardware well

The algorithm is mature, with a standard library or a common library on many platforms

In Python, hashlib can compute it directly

Cons

Memory and parallelism must be tuned together, and a bad setting weakens it

Framework integration is less common than for bcrypt and Argon2

Without a special reason, a new project is better off with Argon2id

Official docs ↗

Checklist

  • The database holds no plaintext or reversibly encrypted passwords
  • Use Argon2id / bcrypt / scrypt / PBKDF2; do not hash directly with MD5/SHA family algorithms
  • Salts are generated by the library and differ per user
  • The login endpoint has rate limiting and lockout after failures
  • Passwords are never written to logs
  • Password reset uses a one-time, time-limited link