Ask a room of junior developers how to store user passwords in a database, and someone will almost always suggest hashing them with SHA-256. On the surface, it sounds reasonable: SHA-256 is a battle-tested cryptographic hash function used across TLS certificates, Git commits, and blockchain networks.
For password storage, however, raw SHA-256 is a major security liability. The exact feature that makes SHA-256 great for checking file integrity—blistering speed—makes it trivial for an attacker to crack when a database table leaks. Here is why dedicated password hashing algorithms like Bcrypt exist, how a Bcrypt hash actually works under the hood, and the implementation traps you need to avoid.
The Speed Problem: Why SHA-256 Is Dangerous for Passwords
General-purpose hash functions like SHA-256 and SHA-512 were engineered to process megabytes of data per second using minimal CPU cycles. When a user downloads a software installer or your server verifies an API webhook signature, you want that hash calculation to finish in microseconds.
When an attacker steals a database of password hashes, that speed works against you. A single consumer graphics card (GPU) can calculate billions of SHA-256 hashes per second. Even if you add a unique random salt to every user's password before running SHA-256, an attacker with a modest GPU rig can brute-force every eight-character password in your database in a matter of hours.
Bcrypt solves this by being intentionally slow and computationally expensive. Built on the Eksblowfish key setup algorithm, Bcrypt forces the hardware to run thousands of sequential memory-access loops for a single password check. Slowing a hash check down to 250 milliseconds is barely noticeable to a human logging in once a day, but it drops an attacker's guessing rate from billions of attempts per second to just a few dozen.
Hashing vs. Encryption vs. Encoding: Picking the Right Tool
Security bugs frequently happen when developers mix up one-way hashing, two-way encryption, and plain text encoding. Each serves a completely different job in your stack:
| Operation | Reversible? | When to Use It | Right Algorithm / Tool |
|---|---|---|---|
| Password Hashing (Slow one-way hash) | No (Irreversible) | Storing user login passwords or password-reset tokens in a database. | Bcrypt, Argon2id (test via Bcrypt Hash Generator & Checker) |
| Data Integrity & Signing (Fast one-way hash) | No (Irreversible) | Verifying downloaded files have not been tampered with, or signing webhook payloads with a secret key. | SHA-256 / SHA-512 via File Hash Generator or HMAC Generator |
| Symmetric Encryption (Two-way with key) | Yes (Requires secret key) | Storing sensitive data your app needs to read back later (such as third-party API keys or tax IDs). | AES-256-GCM / CBC (see AES-256 Encrypt & Decrypt Tool) |
| Encoding (Public format conversion) | Yes (No key needed) | Safely transmitting binary data or special characters inside URLs or JSON strings. Never use for security. | Base64, URL Percent-Encoding |
Anatomy of a 60-Character Bcrypt Hash String
If you hash the password correct-horse-battery with Bcrypt twice in a row, you will get two completely different 60-character output strings. That raises a common question: if the output changes every time, how does your backend verify a user's password on login without storing a separate salt column in the database?
The answer is that a Bcrypt output string is self-contained. Everything needed to verify the password is embedded directly inside the 60 characters:
$2b$12$GhvMmNVjRW29ulnudl.LbuAnUtN/LRfe1JsBm1Xu6LE3059z5Tr8m
| | | |
| | | +-- 31 chars: The resulting 184-bit password hash
| | +------------------------- 22 chars: The random 128-bit salt (modified Base64)
| +---------------------------- 2 chars: Cost factor ("12" = 2^12 = 4,096 rounds)
+------------------------------- 4 chars: Algorithm version prefix ($2b$)
- Algorithm prefix (
$2a$,$2b$, or$2y$): Identifies the Bcrypt specification revision. Modern libraries default to$2b$(or$2y$in PHP'spassword_hash()), both of which fixed an older 2011 character-encoding bug and are fully compatible with each other. - Cost factor (
12): The exponent that controls the work factor ($2^{12}$ iterations). - Embedded 128-bit salt (22 characters): A unique random salt generated automatically when the hash was created. When a user logs in, Bcrypt reads those exact 22 salt characters out of the stored database string, hashes the incoming login password with that same salt and cost factor, and checks whether the resulting 31-character tail matches.
Choosing the Right Cost Factor (Salt Rounds)
In Bcrypt, the "salt rounds" or "cost factor" parameter is exponential, not linear. Increasing the cost factor by just 1 doubles the time required to hash a password:
- Cost 10 ($2^{10} = 1,024$ rounds): Takes roughly 60–90 ms on a modern server CPU. According to the OWASP Password Storage Cheat Sheet,
10is the absolute minimum acceptable setting for Bcrypt. - Cost 12 ($2^{12} = 4,096$ rounds): Takes roughly 250–350 ms. This is the sweet spot for most production web applications today.
- Cost 14 ($2^{14} = 16,384$ rounds): Takes roughly 1 second per login. While ultra-secure against offline cracking, setting your cost factor too high makes your login endpoint vulnerable to Denial-of-Service (DoS) tie-ups if 50 users try to log in at the exact same second.
Aim for the highest cost factor that keeps your login verification time under 300 to 400 milliseconds on your production hardware.
The 72-Byte Truncation Trap (And Why Emojis Make It Worse)
Bcrypt has one hard architectural limit that every developer needs to know: it only processes the first 72 bytes of a password.
Depending on the library you use, passing a password longer than 72 bytes causes one of two problems:
- Silent truncation (PHP, Node.js, Go default implementations): Any characters beyond byte 72 are quietly ignored. If a user sets a 90-character passphrase, changing character 85 will still let them log in—because only the first 72 bytes were hashed.
- Hard runtime exception (Recent Python
bcrypt/passlibversions): The library throws aValueError: password cannot be longer than 72 bytes, crashing your registration route with a500error unless caught.
Notice that the limit is 72 bytes, not 72 characters. Standard English ASCII letters take 1 byte each, but accented European letters take 2 bytes in UTF-8, Arabic characters take 2 bytes, and emojis take 4 bytes each. A password containing 19 emojis is already 76 bytes long! Enforce a sensible maximum byte check (or character limit around 55–64 characters) on your front-end and back-end validation forms so users never hit silent truncation.
Generating and Verifying Bcrypt Hashes in Your Browser
When you are seeding test accounts into a staging database, resetting an admin hash manually in SQL, or debugging a login issue, you should never paste real passwords into a random server-side website that logs form requests.
You can generate and verify Bcrypt hashes locally on your machine using the Bcrypt Hash Generator & Checker:
- Generate a hash: Enter your plaintext test password, pick your desired cost factor (such as
10or12), and generate the 60-character Bcrypt string directly in your browser. - Verify an existing hash: Paste a
$2a$,$2b$, or$2y$hash string alongside a candidate password to confirm whether they match without writing a one-off terminal script. - Inspect API auth payloads safely: If you are debugging authentication responses or webhook payloads containing hashes and tokens, format them client-side using the techniques in our guide on fixing common JSON syntax errors and silent bugs so sensitive credentials never leave your browser.
Frequently Asked Questions (FAQ)
Should I hash the password with SHA-256 first before passing it to Bcrypt to bypass the 72-byte limit?
Be very careful with pre-hashing. As warned in the OWASP Password Storage Cheat Sheet, if you pass raw binary SHA-256 output into a C-based Bcrypt library, any 0x00 null byte in the digest will be treated as the end of the string, truncating the hash early. In addition, if your database leaks, pre-hashing exposes you to "pass-the-hash" shortcuts if not paired with a keyed HMAC. For almost all web apps, enforcing a clear password length limit is safer and simpler.
What is the difference between a Salt and a Pepper?
A salt is a unique random value generated per user and stored right alongside the hash in the database (inside the 60-character Bcrypt string). A pepper is a single secret key shared across all passwords and stored outside the database—such as in an environment variable or hardware security module (HSM)—commonly applied via an HMAC before hashing so a leaked SQL dump alone cannot be cracked without the server's environment secrets.
How do I upgrade existing users from Cost Factor 10 to Cost Factor 12?
Because hashes are one-way, you cannot convert an existing Cost 10 hash into a Cost 12 hash directly in the database. Instead, upgrade hashes lazily during login: when a user submits their plaintext password and it passes verification against their stored $2b$10$ hash, check the cost factor in code (for example, using PHP's password_needs_rehash()), re-hash the plaintext password at Cost 12, and save the new $2b$12$ string back to the database.