Crypto V: Passwords should never be decrypted
The first four parts of this series hid things so they could be read later. Passwords are the opposite: a login only needs to know if a password is right. Why encryption and fast hashes are the wrong tools, and how to store passwords with Argon2id in Python.
Everything in this series so far has been about hiding something so the right person can read it later. age encrypts a file for a recipient, SOPS keeps secrets readable for whoever holds the key, a wormhole carries a file to the other side, and an SSH certificate proves who someone is for a while. Passwords are the odd one out. A login never needs to know what the password is. It only needs to know whether the one just typed is the same as before.
That changes the whole approach. If nobody ever needs to read the password back, then the safest system is one that can't, not even its own administrator.
Why encryption is the wrong tool #
Encryption is reversible by design: with the key, the original comes back. For passwords, that means the database and the key that opens it have to live on the same server, because the login code needs both. Whoever steals one usually steals the other, and then every password comes out at once, in plain text, ready to be tried on email and bank accounts.1
Hashing works the other way. A hash function turns any input into a fixed-size fingerprint, and there's no key that turns it back. To check a login, the server hashes what was typed and compares the result with the stored fingerprint. The password itself is never stored anywhere.
Source
digraph {
rankdir=LR;
node [shape=box, style=rounded, fontname="Helvetica", margin="0.4,0.12"];
edge [fontname="Helvetica"];
typed [label="password typed\nat login"];
hash [label="hash it\nwith the stored salt"];
stored [label="stored hash"];
result [label="equal: welcome\ndifferent: try again"];
typed -> hash;
hash -> result;
stored -> result;
}Fast hashes are the wrong kind #
The hashes most people know, like SHA-256, were built to be fast, which is perfect for checking a downloaded file. It's also exactly what an attacker wants. With a stolen list of hashes, there's nothing to reverse: guess a password, hash it, see if it matches. The faster the hash, the more guesses per second.
To see how fast, this script tries a million guesses with SHA-256 and twenty with Argon2id, the function this post is about, using the default settings of the Python library:
import hashlib, time
from argon2 import PasswordHasher
guesses = [f"password{i}".encode() for i in range(1_000_000)]
start = time.perf_counter()
for g in guesses:
hashlib.sha256(g).digest()
fast = len(guesses) / (time.perf_counter() - start)
ph = PasswordHasher()
start = time.perf_counter()
for g in guesses[:20]:
ph.hash(g)
slow = 20 / (time.perf_counter() - start)
print(f"SHA-256: {fast:,.0f} guesses per second")
print(f"Argon2id: {slow:,.1f} guesses per second")
print(f"Argon2id is {fast / slow:,.0f} times slower")$ pip install argon2-cffi $ python3 speed.py SHA-256: 2,977,497 guesses per second Argon2id: 7.4 guesses per second Argon2id is 403,183 times slower
Source
$ pip install argon2-cffi
$ python3 speed.py
SHA-256: 2,977,497 guesses per second
Argon2id: 7.4 guesses per second
Argon2id is 403,183 times slowerThat's a small cloud machine with two CPU cores, and the numbers change a little on every run.2 Put in terms of a stolen database, the same machine gives this:
| Function | Guesses per second | Time for 1 billion guesses |
|---|---|---|
| SHA-256 | 2,977,497 | about 6 minutes |
| Argon2id | 7.4 | about 4.3 years |
Source
| Function | Guesses per second | Time for 1 billion guesses | |----------+--------------------+----------------------------| | SHA-256 | 2,977,497 | about 6 minutes | | Argon2id | 7.4 | about 4.3 years |
For someone logging in, a hash that takes a seventh of a second is invisible. For someone going through a list of billions of guesses, it's the difference between an afternoon and about two centuries. And attackers don't use one CPU core: they use graphics cards, which are very good at running a simple function like SHA-256 thousands of times in parallel.

Salt: the same password, two different hashes #
There's a second problem with plain hashes. The same password always produces the same hash, so an attacker can hash the most common passwords once, keep the results in a table, and look up every stolen hash in that table instantly. It also means that two users with the same password have the same hash, and cracking one cracks both.3
The fix is a salt: a random value, different for each user, mixed into the hash and stored next to it. The salt isn't secret. It only makes every hash unique, so precomputed tables become useless and every user has to be attacked separately.
The reference Argon2 tool shows what happens when the salt does not change. Here the salt is fixed on purpose, saltsaltsalt, and the password goes in twice:
$ echo -n "correct horse battery staple" | argon2 saltsaltsalt -id -t 2 -k 19456 -p 1 Type: Argon2id Iterations: 2 Memory: 19456 KiB Parallelism: 1 Hash: 338577dcea5a1d0f74bf5a441077f024532e4f15c7135ee3be129e3cbfd2a7cb Encoded: $argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$M4V33OpaHQ90v1pEEHfwJFMuTxXHE17jvhKePL/Sp8s 0.040 seconds Verification ok $ echo -n "correct horse battery staple" | argon2 saltsaltsalt -id -t 2 -k 19456 -p 1 Type: Argon2id Iterations: 2 Memory: 19456 KiB Parallelism: 1 Hash: 338577dcea5a1d0f74bf5a441077f024532e4f15c7135ee3be129e3cbfd2a7cb Encoded: $argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$M4V33OpaHQ90v1pEEHfwJFMuTxXHE17jvhKePL/Sp8s 0.030 seconds Verification ok
Source
$ echo -n "correct horse battery staple" | argon2 saltsaltsalt -id -t 2 -k 19456 -p 1
Type: Argon2id
Iterations: 2
Memory: 19456 KiB
Parallelism: 1
Hash: 338577dcea5a1d0f74bf5a441077f024532e4f15c7135ee3be129e3cbfd2a7cb
Encoded: $argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$M4V33OpaHQ90v1pEEHfwJFMuTxXHE17jvhKePL/Sp8s
0.040 seconds
Verification ok
$ echo -n "correct horse battery staple" | argon2 saltsaltsalt -id -t 2 -k 19456 -p 1
Type: Argon2id
Iterations: 2
Memory: 19456 KiB
Parallelism: 1
Hash: 338577dcea5a1d0f74bf5a441077f024532e4f15c7135ee3be129e3cbfd2a7cb
Encoded: $argon2id$v=19$m=19456,t=2,p=1$c2FsdHNhbHRzYWx0$M4V33OpaHQ90v1pEEHfwJFMuTxXHE17jvhKePL/Sp8s
0.030 seconds
Verification okSame salt, same password, same hash, byte for byte. That's fine for a demonstration and a disaster in a real database. In practice nobody picks the salt by hand: the library generates a new random one every time, as the Python example below shows. The argon2 command comes from the argon2 package on Debian and Ubuntu.
Argon2id #
Between 2013 and 2015, cryptographers ran the Password Hashing Competition, an open contest to find a function made for passwords. The winner, in 2015, was Argon2, by Alex Biryukov, Daniel Dinu and Dmitry Khovratovich from the University of Luxembourg. In September 2021 it became RFC 9106.
Argon2 is slow on purpose, and it's slow in a specific way: it needs memory. Every guess has to fill and read a large block of RAM, and while a graphics card has thousands of cores, it doesn't have thousands of times the memory to give each one. That takes away most of the advantage attackers had with SHA-256.
It comes in three variants. Argon2d resists graphics cards best, Argon2i resists side-channel attacks, where someone learns about the password by watching how the computer behaves, and Argon2id mixes the two. The RFC makes Argon2id the one every implementation must support and the default choice when in doubt. For passwords, that's the answer.
It takes three main settings:
- m: how much memory each hash uses, in KiB
- t: how many passes it makes over that memory
- p: how many lanes run in parallel
Storing passwords in Python #
In Python, the usual library is argon2-cffi. It takes care of the salt, the encoding and the comparison, so the code stays short:
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
ph = PasswordHasher()
print(ph.time_cost, ph.memory_cost, ph.parallelism)
stored = ph.hash("correct horse battery staple")
print(stored)
print(ph.hash("correct horse battery staple"))
print(ph.verify(stored, "correct horse battery staple"))
try:
ph.verify(stored, "correct horse battery stapler")
except VerifyMismatchError:
print("wrong password")
stronger = PasswordHasher(time_cost=4)
print(stronger.check_needs_rehash(stored))$ python3 store.py 3 65536 4 $argon2id$v=19$m=65536,t=3,p=4$/jhIIzsdnJI5gnU+wwenmA$7MdlbA4nf26pYLHnzikcQsUe2a4VB/IIACGttI7eR34 $argon2id$v=19$m=65536,t=3,p=4$b1/GAET7pzkY6EMuFRjANg$ewEbp1V+rGjgTe/KjIswH0b+73IYn1cTsl95ubbvjmQ True wrong password True
Source
$ python3 store.py
3 65536 4
$argon2id$v=19$m=65536,t=3,p=4$/jhIIzsdnJI5gnU+wwenmA$7MdlbA4nf26pYLHnzikcQsUe2a4VB/IIACGttI7eR34
$argon2id$v=19$m=65536,t=3,p=4$b1/GAET7pzkY6EMuFRjANg$ewEbp1V+rGjgTe/KjIswH0b+73IYn1cTsl95ubbvjmQ
True
wrong password
TrueGoing through it:
- The defaults are 3 passes, 65,536 KiB (64 MiB) of memory and 4 lanes, which is the second of the two settings the RFC recommends.
- The same password, hashed twice, gives two different strings. That's the random salt at work.
- The stored string carries everything needed to check it later, separated by
$. It fits in a single text column, and there's no separate salt column to manage.4 -
verifyreturnsTrueor raisesVerifyMismatchError. The order of the arguments is the stored hash first and the typed password second, and swapping them is a common mistake. -
check_needs_rehashsays whether a stored hash was made with weaker settings than the current ones. Here the new hasher uses 4 passes, so the old hash needs an upgrade.
Taking the first stored string apart, piece by piece:
| Piece | What it means |
|---|---|
| argon2id | the variant |
| v=19 | the version of Argon2 |
| m=65536,t=3,p=4 | memory in KiB, passes and lanes |
| /jhIIzsdnJI5... | the random salt, in base64 |
| 7MdlbA4nf26p... | the hash itself, in base64 |
Source
| Piece | What it means | |-----------------+---------------------------------------------------| | argon2id | the variant | | v=19 | the version of Argon2 | | m=65536,t=3,p=4 | memory in KiB, passes and lanes | | /jhIIzsdnJI5... | the random salt, in base64 | | 7MdlbA4nf26p... | the hash itself, in base64 |
The upgrade from check_needs_rehash matters because a password can't be rehashed without knowing it. The only moment the server sees it is at login, so that's when the upgrade happens:
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
users = {"ana": PasswordHasher(time_cost=2).hash("correct horse battery staple")}
ph = PasswordHasher()
def login(name, password):
stored = users.get(name)
if stored is None:
return False
try:
ph.verify(stored, password)
except VerifyMismatchError:
return False
if ph.check_needs_rehash(stored):
users[name] = ph.hash(password)
return True
print(users["ana"][:31])
print(login("ana", "correct horse battery staple"))
print(users["ana"][:31])
print(login("ana", "Tr0ub4dor&3"))$ python3 login.py $argon2id$v=19$m=65536,t=2,p=4$ True $argon2id$v=19$m=65536,t=3,p=4$ False
Source
$ python3 login.py
$argon2id$v=19$m=65536,t=2,p=4$
True
$argon2id$v=19$m=65536,t=3,p=4$
FalseAna's hash was made with 2 passes. After one successful login it's stored again with 3, and she never noticed. A dictionary stands in for the database here, but the logic is the same with a real one. One improvement before using this for real: when the user doesn't exist, login returns at once, without hashing anything. That answer comes back much faster than a wrong password, and someone timing the responses can find out which names have accounts. Hashing a dummy password in that branch keeps both paths equally slow.
Choosing the numbers #
There's no single right setting, because it depends on the machine that runs the login. Two references help:
- RFC 9106 recommends 1 pass, 4 lanes and 2 GiB of memory where the server can afford it, and 3 passes, 4 lanes and 64 MiB where memory is tight. The second one is what argon2-cffi uses by default.
- The OWASP Password Storage Cheat Sheet lists minimums that trade memory for passes, all equally strong by its account. One of them is 19 MiB of memory, 2 passes and 1 lane, the settings used in the
argon2example above.
The method is simple: start from one of those, measure how long a hash takes on the real server, and raise the memory until a login takes as long as people can reasonably wait. Then let check_needs_rehash move the old users over as they log in.
What hashing can't fix #
Argon2id makes each guess expensive, but it can't make a bad password good. If the password is 123456, it will be in the first few guesses of any list, and no amount of memory saves it. That's where password managers and long passphrases come in5, which is a problem for users, not for the database.

On the server side, one more layer is a pepper: a secret value added to every password before hashing, kept outside the database, in a configuration file or a secrets manager. A leaked database without the pepper is useless for guessing. It adds a key to manage and doesn't replace anything above, so it's an extra, not a starting point.
Next time a site offers to email you your old password, that's a sign it was stored the wrong way.
Notes
In 2013 Adobe lost about 153 million accounts whose passwords were encrypted with one key in ECB mode, instead of hashed. Equal passwords became equal ciphertexts, and the password hints stored in plain text next to them gave away the rest. ↩
The SHA-256 loop runs on one core. Argon2id was set to 4 lanes, so it could use both cores, and each hash filled 64 MiB of memory. ↩
In 2012, about 6.5 million LinkedIn password hashes were posted online. They were SHA-1 without salt, and many of them were cracked soon after. ↩
v=19is the version of Argon2 in decimal. In hexadecimal it's0x13, which is version 1.3. ↩The two passwords in the examples, "correct horse battery staple" and "Tr0ub4dor&3", come from xkcd 936, published in August 2011, which compares a short password full of symbols with four random common words. ↩