Password hashing

On this page 7

Password hashing is the practice of storing a password not as plain text but as the output of a deliberately slow, salted one-way function, so that the system can verify a login without ever keeping the password itself. In cryptography, it is a specialized application of hashing designed for one goal: making offline guessing of stolen password databases as expensive as possible.

A general-purpose hash function such as SHA-256 is built to be fast, which is exactly wrong for passwords. Dedicated password hashing functions such as Argon2, scrypt, bcrypt and PBKDF2 add a unique salt and a tunable work factor, turning each guess into a measurable cost in time and memory for any attacker.

How password hashing protects stored credentials

Password hashing protects credentials by ensuring that a database breach yields verifiers, not passwords. When a user registers, the server generates a random salt, combines it with the password, runs the result through the password hashing function and stores the salt, the parameters and the resulting digest together.

At login, the server repeats the same computation with the stored salt and parameters and compares the result with the stored digest using a constant-time comparison. Because the function is one-way, an attacker who copies the database must guess candidate passwords and hash each one, paying the full cost of the function for every attempt.

The table below lists the components found in a typical stored password record and the role each plays.

ComponentTypical size or valuePurpose
Salt16 bytes, random per userMakes identical passwords produce different hashes
Work factorIterations, memory, parallelismSets the cost of each guess
Digest32 bytes or moreThe value compared at login
Algorithm identifierFor example $argon2id$Allows verification and later upgrades
PepperSecret key outside the databaseAdds protection if only the database leaks
Components of a stored password hash

Formats such as the PHC string format or the modular crypt format pack all of these values into one string, which lets applications upgrade parameters gradually by rehashing a password the next time its owner signs in successfully.

Password hashing algorithms and their parameters

Modern password hashing algorithms differ mainly in whether they are memory-hard, which determines how well they resist attackers using GPUs and custom hardware. Memory hardness forces each guess to use a large block of RAM, which is expensive to replicate across thousands of parallel cores.

AlgorithmYearMemory-hardMain tuning parameters
Argon2id2015YesMemory, iterations, parallelism
scrypt2009YesCPU/memory cost N, block size r, parallelism p
bcrypt1999Limited, 4 KiBCost factor as a power of two
PBKDF22000NoIteration count and underlying HMAC
Main password hashing algorithms

Argon2id, the winner of the Password Hashing Competition and specified in RFC 9106, is the current first choice for new systems. PBKDF2 remains relevant because it is approved under FIPS 140 validation regimes, which matters in government and regulated environments, but it needs very high iteration counts to compensate for its lack of memory hardness.

bcrypt is still widely deployed and reasonably strong, although it has a 72-byte input limit: characters beyond that length are silently ignored, which can surprise developers who prehash or concatenate values.

Password hashing is often confused with general hashing and with encryption, yet each technique serves a different purpose. The comparison below shows why only a dedicated password hashing function is appropriate for stored credentials.

TechniqueReversibleSpeedSuitable for passwords
Password hashingNoDeliberately slowYes, the intended use
Fast hash such as SHA-256NoBillions per second on GPUsNo, guessing is too cheap
Symmetric encryptionYes, with the keyFastNo, key theft reveals all passwords
Key derivation for encryptionNoConfigurableRelated, same functions often reused
Password hashing versus related techniques

The distinction between hashing and encryption is decisive: an encrypted password table is only as safe as its key management, and a single key compromise exposes every password. Hashing removes that single point of failure, because no key exists that reverses the digests.

Choosing parameters for production systems

Parameters should be chosen so that one verification takes roughly 100 to 500 milliseconds on the production server, which is unnoticeable to a user but multiplies attacker cost enormously. The right values depend on hardware, login volume and acceptable server load.

Widely cited baselines from the OWASP password storage guidance give a practical starting point. Teams should benchmark on their own hardware and raise the parameters as servers get faster, typically reviewing them every year or two.

AlgorithmSuggested minimum settingNotes
Argon2id19 MiB memory, 2 iterations, 1 laneIncrease memory first if capacity allows
scryptN of 2^17, r of 8, p of 1Uses about 128 MiB per hash
bcryptCost factor of 10 or higherWatch the 72-byte input limit
PBKDF2-HMAC-SHA256600,000 iterationsUse only when compliance requires it
Starting parameters for common algorithms
  • Use a well-maintained library rather than implementing the algorithm yourself.
  • Generate salts with a cryptographically secure random generator, never from usernames or timestamps.
  • Store a pepper in a hardware security module or secrets manager, separate from the database.
  • Rate limit login endpoints, because slow hashing increases denial-of-service exposure.

Common mistakes and failure modes

Most real-world password breaches that led to mass cracking involved avoidable implementation errors rather than flaws in modern algorithms. The table below lists the mistakes security reviewers encounter most often.

MistakeConsequenceCorrect practice
Unsalted hashesRainbow tables crack many accounts at onceUnique random salt per password
Fast hash functionsGPU cracking at enormous speedUse Argon2id, scrypt or bcrypt
Static low work factorProtection erodes as hardware improvesReview and raise parameters regularly
Logging plaintext passwordsHashing is bypassed entirelyScrub credentials from logs and traces
Non-constant-time comparisonTiming side channelsUse library verification functions
Password hashing mistakes and their consequences

Password hashing also has inherent limits. It cannot protect a weak password such as a common dictionary word, because the attacker will try it within the first few guesses regardless of cost. It also offers no defense when credentials are phished or reused from another breach, which is why MFA and breached password screening remain necessary companions.

A plain-language summary

Password hashing means a website never keeps your actual password. Instead, it keeps a scrambled fingerprint of it that cannot be turned back into the original. When you sign in, the site scrambles what you typed the same way and checks whether the fingerprints match.

The scrambling is made slow on purpose. For you, waiting a fraction of a second is unnoticeable. For a thief who stole the database and wants to test millions of guesses, that small delay adds up to years of work. The key takeaway is that good hashing buys time, but a strong, unique password is still what makes guessing fail.

An example from practice

An online learning platform with 2 million user accounts discovered during a security audit that its legacy code stored passwords as unsalted SHA-1 hashes. Testing on a single modern GPU showed that a large share of the hashes could be cracked within hours using common password lists.

The engineering team adopted Argon2id with 64 MiB of memory and three iterations, which took about 200 milliseconds per login on their servers. Because they could not recover the original passwords, they wrapped each existing SHA-1 digest inside Argon2id immediately and replaced it with a direct Argon2id hash at each user’s next successful sign-in. Within six months most active accounts had migrated, and the cost of cracking each remaining hash rose by several orders of magnitude.