Bcrypt

Hash and compare text string using bcrypt. Bcrypt is a password-hashing function based on the Blowfish cipher.

Enable JavaScript to use the interactive tool. The guide below explains how it works.

Bcrypt: Technical Deep Dive for Developers and Sysadmins

Bcrypt is an adaptive, salted password-hashing function based on the Blowfish block cipher. It is specifically engineered to remain secure against brute-force and hardware-accelerated attacks (FPGA/ASIC) by introducing a configurable "cost factor" that slows down the hashing process.

1. What the Tool Does

Bcrypt implements a modified version of the Blowfish cipher called EksBlowfish (Expensive Key Schedule Blowfish). Unlike standard hashing algorithms (like SHA-256) which are designed for speed, bcrypt is designed to be computationally expensive.

It transforms a plaintext password into a secure hash by:

2. Why Someone Uses It

Developers use bcrypt to protect user credentials in the event of a database breach.

3. Step-by-Step Instructions

Implementation Workflow

  1. Registration:
    • Receive plaintext password from the user.
    • Generate a random 16-byte salt.
    • Set a cost factor (e.g., 12).
    • Run bcrypt(password, salt, cost) $\rightarrow$ store the resulting string in the database.
  2. Authentication:
    • Retrieve the stored hash for the user.
    • Extract the salt and cost factor from the hash string (they are encoded in the prefix).
    • Hash the provided plaintext password using the extracted salt and cost.
    • Perform a constant-time comparison between the new hash and the stored hash.

Advanced Usage: Handling the 72-Byte Limit

Bcrypt has a hard limit of 72 bytes for the input password. Any characters beyond this are silently ignored. To support longer passwords without losing entropy:

  1. Hash the password with a fast, collision-resistant hash (e.g., SHA-256).
  2. Pass the resulting binary hash (or its Base64 encoding) into bcrypt.
  3. Warning: If using this method, ensure you use a consistent encoding to avoid issues with null bytes in the SHA-256 output.

4. Examples

Anatomy of a Bcrypt Hash

$2b$12$R9h/cIPz0gi.S5S9vT5fL.n6Zp2S9f2z8S7fS0S5S0S5S0S5S0S5S

Code Example (Conceptual Node.js)

const bcrypt = require('bcrypt');
const saltRounds = 12;

// Hashing
const hash = await bcrypt.hash(plaintextPassword, saltRounds);

// Verification (Uses constant-time comparison internally)
const match = await bcrypt.compare(plaintextPassword, hash);

5. FAQs

Q: What is the ideal cost factor? A: The cost factor should be as high as your server can tolerate without degrading user experience. A common target is a hashing time of ~250ms to 500ms.

Q: Should I use a separate salt column in my database? A: No. Bcrypt encodes the salt and cost factor directly into the resulting hash string, making it self-contained.

Q: Is bcrypt better than Argon2? A: Argon2 is the winner of the Password Hashing Competition and is generally superior because it is memory-hard (resisting GPU/ASIC attacks better than bcrypt). However, bcrypt remains a high-value, industry-standard choice with broader library support.

6. Common Mistakes

7. Use Cases (Professional/Industrial)

8. Related Resources

All tools

Guides and articles