Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Security › Authentication

Password hashing

How well do passwords hold up when the database leaks?

Why it exists — If passwords are kept in plain text, they are exposed as they are the moment the DB leaks, and even if they are stored converted with a fast hash such as SHA-256, an attacker tries common passwords very quickly. bcrypt automatically attaches a different salt for each member and deliberately raises the computation cost, which increases the time each guess takes and slows down brute-force attacks.

Where it's used today — It is used to store passwords in a service that has sign-up and login. In Spring Security, passwords are converted and stored with a PasswordEncoder and compared with matches at login, and BCryptPasswordEncoder is a common choice. Information that must later be turned back into its original value (such as external API keys) is stored encrypted, not hashed.

Two members happen to use the same password (password123). How the passwords are stored in the database greatly changes how long an attacker needs to recover the original passwords after the database leaks. A hash is a value computed from the original in a way that cannot be reversed. This lab compares plain text, a hash, a salted hash, and a deliberately slow hash (bcrypt).

Checked against real execution · All 12 combinations run and checked on JDK 21 with Spring Security 7.1.1 (the times are measured values)

Concepts in this lab

Hash function and salt
A hash function turns an input into a fixed-length value, and the original input cannot be recovered from the result. For passwords, the hash value is stored instead of the plain text, so the plain text is not exposed even if the data leaks. A salt is a random value that is added to each password and is different for each one. It makes the same password produce a different hash, which blocks attacks that use precomputed lists of hashes.
bcrypt
Where it's used today — It is used when storing member passwords. The default encoder created by Spring Security's PasswordEncoderFactories also stores new passwords with bcrypt, and other choices for the same purpose include Argon2, scrypt, and PBKDF2.
A hash function designed for storing passwords. It generates a salt automatically and includes it in the result, and it lets you set a work factor (cost) that makes the computation slow on purpose. A slow computation greatly increases the time an attacker needs to try a huge number of candidates. Spring Security's BCryptPasswordEncoder uses it.
Symmetric-key cryptography and public-key cryptography
Where it's used today — In HTTPS (TLS), public-key cryptography is used when the connection is set up, to verify the signature of the server certificate and to exchange keys, and the HTTP data that follows is encrypted with a symmetric key such as AES. Values stored in a database, such as card numbers, are encrypted with a symmetric key, and the key pair used for SSH login is a public-key one.
Symmetric-key cryptography uses the same key to encrypt and to decrypt. It is fast, but it has the problem that the key must be shared safely with the other party (AES). Public-key cryptography uses a pair of keys, a public key and a private key. Data encrypted with the public key can be decrypted only with the private key, so there is no key-sharing problem, but it is slow (RSA). In practice, public-key cryptography is used to exchange a symmetric key safely, and then the symmetric key encrypts the data.

Try these

Running with the default settings

  1. Alice and Bob sign up, and both choose password123 as their password. The server stores it in the database using the SHA-256 method.
  2. The stored values of the two members are exactly the same. Anyone who sees the database can tell at once that the two passwords are the same, and recovering one gives the passwords of every member with the same value.
  3. Incident: an attacker has copied the whole member table out of the database.
  4. A hash cannot be computed backward, but the hashes of common passwords can be computed in advance and kept in a table. The attacker only has to look the stored value up in the table, because the SHA-256 of password123 is the same value for everyone.
  5. At the speed measured on one CPU core, trying 100 million common passwords against one member takes about 11 seconds. A common password near the front of the dictionary, such as password123, is found almost instantly.

Result of this run

Code

// Only lines inside a method are shown (declarations and imports omitted; member and rawPassword are example variables)
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(rawPassword.getBytes(UTF_8));
member.setPassword(HexFormat.of().formatHex(hash));

Assumptions of this model

The SHA-256 values and the bcrypt stored values are ones that MessageDigest of JDK 21 and BCryptPasswordEncoder of Spring Security 7.1.1 actually produced in the verification run (the salts x7Kp2q and M4nT9z and the member names are made up for this example). bcrypt generates a new salt on every run, so its values change. The values from the first run are fixed on screen and are checked again with matches on every run. "Reverse lookup with a precomputed table" is the result of actually looking the stored values up in a table of the SHA-256 hashes of 1 million words (only the unsalted SHA-256 was found). The dictionary attack, which tries a list of common passwords against a stored value one by one, was also actually run, and it found password123, the 20th word, for SHA-256, salted SHA-256, and bcrypt alike. The times were measured with JDK 21 on one CPU core of this computer (AMD Ryzen 5 5600). SHA-256 ran about 9 million times per second, and one call of matches on BCryptPasswordEncoder took 58.3ms at cost 10, 240.5ms at 12, and 1090.8ms at 14 (the fastest of several measurements). Each increase of the cost by 1 multiplied the time by 1.95 to 2.17. The time for "trying 100 million passwords" is calculated by multiplying this speed by 100 million. For SHA-256, 100 million hashes were actually computed, which took 11.8 seconds. For bcrypt, the time is only a calculation, and 100 million attempts were not run. These numbers vary with the CPU and from run to run. Real attackers guess much faster than this with several graphics cards, but that speed was not measured. That the default cost of BCryptPasswordEncoder is 10, and the structure of the stored value (60 characters), were also verified by execution.

Previous lab: Cookies · Next lab: JWT structure and verification

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these