Bcrypt is one of the few password hashing functions that handles salts entirely automatically. It generates the salt, embeds it in the output, and uses it during verification without any developer intervention. But what is happening inside that hash string?
For a conceptual overview of what salts are and why they matter for password security, see our companion article: What Is a Salt and Why It Matters for Password Security. This guide focuses on the bcrypt-specific implementation: the hash format, how the salt is embedded, and how to work with it in code.
The Bcrypt Hash String Format
When you hash a password with bcrypt, the output is a single self-contained string that includes everything needed to verify the password later. Here is what it looks like:
$2b$12$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy
Breaking it down into its components:
- **$2b$** — the bcrypt version ($2a$, $2b$, or $2y$ depending on the library) - **12** — the cost factor (2^12 = 4,096 iterations) - **N9qo8uLOickgx2ZMRZoMyeI** — the 22-character base64-encoded salt (16 bytes) - **IjZAgcfl7p92ldGxad68LJZdL17lhWy** — the 31-character base64-encoded hash
The salt is not stored in a separate column. It is not stored in a separate file. It is right there in the hash string. When you verify a password, bcrypt reads the salt and cost factor from the stored hash, re-hashes the submitted password with those same parameters, and compares the output.
How Bcrypt Handles Salts
Here is the best part about bcrypt: it handles salts automatically. You do not need to generate a salt, store it separately, or manage it in any way. The salt is generated and embedded in the hash string itself.
When you call bcrypt to hash a password, here is what happens behind the scenes:
1. Bcrypt generates a 16-byte cryptographically random salt using your system's secure random number generator. 2. It combines the salt with the password and runs the Blowfish-based key scheduling algorithm for 2^cost iterations. 3. It produces a hash string that contains the version, cost factor, salt, and hash all in one string.
The output looks like this:
$2b$12$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy
Breaking it down: - **$2b$** — version - **12** — cost factor - **N9qo8uLOickgx2ZMRZoMyeI** — the 22-character salt (16 bytes encoded in base64) - **IjZAgcfl7p92ldGxad68LJZdL17lhWy** — the 31-character hash
When a user logs in and you need to verify their password, bcrypt reads the salt and cost factor from the stored hash, re-hashes the submitted password with those same parameters, and compares the output to the stored hash. If they match, the password is correct.
You never write code to generate, store, retrieve, or compare salts. The library handles all of it. You just call hash and verify.
Why You Do Not Store the Salt Separately
A common question is: "If the salt is in the hash string, is it not exposed to attackers? Shouldn't I store it separately for extra security?"
The answer is no, and here is why.
Salts are not secret. Their purpose is not to add entropy to the password. Their purpose is to ensure that identical passwords produce different hashes. Even if an attacker knows the salt (and they do, because it is in the hash string), they still cannot use rainbow tables, because they still need to compute the hash for each unique salt from scratch.
Think of it this way: the salt is like a name tag. It identifies which specific hash corresponds to which user. It is not a secret code. The security comes from the slowness of the hashing algorithm, not from keeping the salt hidden.
Storing the salt separately actually makes things worse in some cases. It adds complexity, it creates another column that could be exposed in a breach, and it does not provide any additional security. Just let bcrypt embed it in the hash string.
Salt vs Pepper: What Is the Difference?
People often confuse salts with peppers. They are related concepts but serve different purposes.
A **salt** is unique per password, generated randomly, and stored alongside the hash. Its job is to make sure identical passwords produce different hashes and to prevent rainbow table attacks.
A **pepper** is a single secret value shared across all passwords in the application. It is not stored in the database. It is stored in application configuration, an environment variable, or ideally a hardware security module. Its job is to add protection in the scenario where the database is compromised but the application server is not.
With a pepper, you hash the password plus the pepper plus the salt. An attacker who steals only the database cannot crack the hashes because they do not know the pepper. They would also need to compromise the application server.
Bcrypt does not have built-in pepper support, but you can implement it by appending a pepper to the password before passing it to bcrypt. Just be careful: if you use a pepper, losing it means every password becomes unverifiable. Store it somewhere safe.
The Bottom Line
Salts are one of those security features that work so seamlessly you might forget they exist. Bcrypt generates a unique random salt for every password, embeds it in the hash string, and uses it automatically during verification. You do not need to think about salts at all.
But understanding why salts exist helps you appreciate why bcrypt is so effective. The combination of a unique per-password salt (defeating rainbow tables) and a configurable cost factor (defeating brute force) is what has made bcrypt the standard for password hashing for over 25 years.
For a broader look at how bcrypt works, including the full hash format breakdown, read our bcrypt explained post. And if you want to see salts in action, try our bcrypt generator and hash the same password multiple times to see how the salt changes the output every time.