> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/hashing/18_bcrypt.md).

# bcrypt

## What is bcrypt?

bcrypt is a **password hashing function** designed by Niels Provos and David Mazières in 1999, based on the Blowfish cipher. It is specifically designed to be **slow and computationally expensive**, making brute-force attacks difficult.

***

## Why bcrypt?

* Built-in **salt** generation (prevents rainbow table attacks)
* **Adaptive cost factor** (work factor can be increased as hardware improves)
* Widely supported across languages and frameworks
* Time-tested and battle-hardened

***

## How bcrypt Works

```
password + salt → bcrypt(cost factor) → 60-char hash string
```

### Hash Format

```
$2b$12$SSSSSSSSSSSSSSSSSSSSSSHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
│   │  │                    │
│   │  └── Salt (22 chars)  └── Hash (31 chars)
│   └── Cost factor (work factor)
└── Version ($2a$, $2b$, $2y$)
```

### Example Output

```
$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewdBPj6y8T9G6hm6
```

***

## Cost Factor (Work Factor)

| Cost | Approx. Time (modern CPU) |
| ---- | ------------------------- |
| 10   | \~100ms                   |
| 12   | \~400ms                   |
| 14   | \~1.5s                    |
| 16   | \~6s                      |

Higher cost = slower hashing = harder to crack.

***

## Implementation Examples

### Python

```python
import bcrypt

# Hashing
password = b"mysecretpassword"
salt = bcrypt.gensalt(rounds=12)
hashed = bcrypt.hashpw(password, salt)
print(hashed)

# Verifying
if bcrypt.checkpw(password, hashed):
    print("Password matches!")
```

### Node.js

```javascript
const bcrypt = require('bcrypt');

// Hashing
const saltRounds = 12;
const hash = await bcrypt.hash('mysecretpassword', saltRounds);

// Verifying
const match = await bcrypt.compare('mysecretpassword', hash);
console.log(match); // true
```

### Go

```go
import "golang.org/x/crypto/bcrypt"

// Hashing
hash, err := bcrypt.GenerateFromPassword([]byte("password"), 12)

// Verifying
err = bcrypt.CompareHashAndPassword(hash, []byte("password"))
```

***

## Internals: EksBlowfish Algorithm

1. Takes password + salt + cost
2. Runs `2^cost` rounds of key setup
3. Encrypts the string `"OrpheanBeholderScryDoubt"` 64 times
4. Returns the resulting ciphertext as the hash

***

## Limitations

* Max password length: **72 bytes** (longer passwords are silently truncated)
* Not ideal for bulk data encryption (designed only for passwords)
* Slower than Argon2 in memory-hardness

***

## bcrypt vs Other Password Hashing Functions

| Feature        | bcrypt | scrypt | Argon2  |
| -------------- | ------ | ------ | ------- |
| Salt built-in  | ✅      | ✅      | ✅       |
| Memory-hard    | ❌      | ✅      | ✅       |
| GPU resistance | Medium | High   | Highest |
| Year           | 1999   | 2009   | 2015    |
| PHC Winner     | ❌      | ❌      | ✅       |

***

## Attacks Against bcrypt

| Attack                      | Effectiveness                      |
| --------------------------- | ---------------------------------- |
| Brute Force                 | Very slow due to cost factor       |
| Rainbow Tables              | Prevented by built-in salt         |
| GPU Acceleration            | Limited (bcrypt is GPU-unfriendly) |
| Password truncation exploit | Possible if passwords > 72 bytes   |

***

## Best Practices

* Use cost factor **12+** for production
* Always use the **verify function** (never re-hash to compare)
* Update cost factor periodically as hardware improves
* Handle the **72-byte limit** by pre-hashing with SHA-256 if needed


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/cryptography/hashing/18_bcrypt.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
