|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) |
| 3 | +// |
| 4 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | +// |
| 7 | +// Official repository: https://github.com/cppalliance/http |
| 8 | +// |
| 9 | + |
| 10 | += BCrypt Password Hashing |
| 11 | + |
| 12 | +This page explains how to securely hash and verify passwords using bcrypt. |
| 13 | + |
| 14 | +NOTE: Code snippets assume `using namespace boost::http;` is in effect. |
| 15 | + |
| 16 | +== What is BCrypt? |
| 17 | + |
| 18 | +BCrypt is a password-hashing function designed by Niels Provos and David |
| 19 | +Mazières. It incorporates: |
| 20 | + |
| 21 | +* A **salt** to protect against rainbow table attacks |
| 22 | +* An **adaptive cost factor** that can be increased as hardware improves |
| 23 | +* Built-in work factor that makes brute-force attacks expensive |
| 24 | + |
| 25 | +BCrypt is the recommended algorithm for password storage. |
| 26 | + |
| 27 | +== Quick Start |
| 28 | + |
| 29 | +[source,cpp] |
| 30 | +---- |
| 31 | +#include <boost/http/bcrypt.hpp> |
| 32 | +
|
| 33 | +// Hash a password |
| 34 | +bcrypt::result hash = bcrypt::hash("my_password", 12); |
| 35 | +
|
| 36 | +// Store hash.str() in database... |
| 37 | +
|
| 38 | +// Later, verify the password |
| 39 | +system::error_code ec; |
| 40 | +bool valid = bcrypt::compare("my_password", stored_hash, ec); |
| 41 | +
|
| 42 | +if (ec) |
| 43 | + // Hash was malformed |
| 44 | +else if (valid) |
| 45 | + // Password matches |
| 46 | +else |
| 47 | + // Password does not match |
| 48 | +---- |
| 49 | + |
| 50 | +== Hashing Passwords |
| 51 | + |
| 52 | +The `hash` function generates a salted hash: |
| 53 | + |
| 54 | +[source,cpp] |
| 55 | +---- |
| 56 | +// Default cost factor (10) |
| 57 | +bcrypt::result r1 = bcrypt::hash("password"); |
| 58 | +
|
| 59 | +// Custom cost factor |
| 60 | +bcrypt::result r2 = bcrypt::hash("password", 12); |
| 61 | +
|
| 62 | +// Custom cost factor and version |
| 63 | +bcrypt::result r3 = bcrypt::hash("password", 12, bcrypt::version::v2b); |
| 64 | +---- |
| 65 | + |
| 66 | +=== Cost Factor |
| 67 | + |
| 68 | +The cost factor (rounds) determines how expensive hashing is. Each increment |
| 69 | +doubles the work: |
| 70 | + |
| 71 | +[cols="1,2"] |
| 72 | +|=== |
| 73 | +| Cost | Approximate Time (modern CPU) |
| 74 | + |
| 75 | +| 10 |
| 76 | +| ~100ms |
| 77 | + |
| 78 | +| 12 |
| 79 | +| ~400ms |
| 80 | + |
| 81 | +| 14 |
| 82 | +| ~1.6s |
| 83 | + |
| 84 | +| 16 |
| 85 | +| ~6.4s |
| 86 | +|=== |
| 87 | + |
| 88 | +**Guidelines:** |
| 89 | + |
| 90 | +* Minimum: 10 for new applications |
| 91 | +* Recommended: 12 for most applications |
| 92 | +* Maximum: 31 (impractically slow) |
| 93 | +* Adjust based on your hardware and latency requirements |
| 94 | + |
| 95 | +=== Password Length Limit |
| 96 | + |
| 97 | +BCrypt only uses the first 72 bytes of a password. Longer passwords are |
| 98 | +silently truncated. If you need to support longer passwords, pre-hash |
| 99 | +with SHA-256: |
| 100 | + |
| 101 | +[source,cpp] |
| 102 | +---- |
| 103 | +// For passwords > 72 bytes |
| 104 | +std::string pre_hash = sha256(long_password); |
| 105 | +bcrypt::result r = bcrypt::hash(pre_hash, 12); |
| 106 | +---- |
| 107 | + |
| 108 | +== Verifying Passwords |
| 109 | + |
| 110 | +The `compare` function extracts the salt from a stored hash, re-hashes |
| 111 | +the input password, and compares: |
| 112 | + |
| 113 | +[source,cpp] |
| 114 | +---- |
| 115 | +system::error_code ec; |
| 116 | +bool valid = bcrypt::compare(user_input, stored_hash, ec); |
| 117 | +
|
| 118 | +if (ec == bcrypt::error::invalid_hash) |
| 119 | +{ |
| 120 | + // Hash string is malformed - data corruption or tampering |
| 121 | + log_security_event("invalid hash format"); |
| 122 | + return false; |
| 123 | +} |
| 124 | +
|
| 125 | +if (valid) |
| 126 | + grant_access(); |
| 127 | +else |
| 128 | + reject_login(); |
| 129 | +---- |
| 130 | + |
| 131 | +WARNING: Always check the error code. A false return value alone does not |
| 132 | +distinguish between "wrong password" and "malformed hash". |
| 133 | + |
| 134 | +== Working with Salts |
| 135 | + |
| 136 | +You can generate and use salts separately: |
| 137 | + |
| 138 | +[source,cpp] |
| 139 | +---- |
| 140 | +// Generate a salt |
| 141 | +bcrypt::result salt = bcrypt::gen_salt(12); |
| 142 | +
|
| 143 | +// Hash with explicit salt |
| 144 | +system::error_code ec; |
| 145 | +bcrypt::result hash = bcrypt::hash("password", salt.str(), ec); |
| 146 | +---- |
| 147 | + |
| 148 | +This is rarely needed since `hash()` generates a salt automatically. |
| 149 | + |
| 150 | +== The result Type |
| 151 | + |
| 152 | +`bcrypt::result` is a fixed-size buffer (no heap allocation): |
| 153 | + |
| 154 | +[source,cpp] |
| 155 | +---- |
| 156 | +bcrypt::result r = bcrypt::hash("password", 12); |
| 157 | +
|
| 158 | +// Access the hash string |
| 159 | +core::string_view sv = r.str(); // Or just use r (implicit conversion) |
| 160 | +char const* cstr = r.c_str(); // Null-terminated |
| 161 | +
|
| 162 | +// Check for valid result |
| 163 | +if (r) |
| 164 | + store(r.str()); |
| 165 | +---- |
| 166 | + |
| 167 | +== Hash String Format |
| 168 | + |
| 169 | +A bcrypt hash string has this format: |
| 170 | + |
| 171 | +---- |
| 172 | +$2b$12$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy |
| 173 | +│ │ │ │ |
| 174 | +│ │ │ └─ hash (31 chars) |
| 175 | +│ │ └─ salt (22 chars) |
| 176 | +│ └─ cost factor |
| 177 | +└─ version |
| 178 | +---- |
| 179 | + |
| 180 | +Total length: 60 characters. |
| 181 | + |
| 182 | +== Extracting the Cost Factor |
| 183 | + |
| 184 | +To check the cost factor of an existing hash: |
| 185 | + |
| 186 | +[source,cpp] |
| 187 | +---- |
| 188 | +system::error_code ec; |
| 189 | +unsigned rounds = bcrypt::get_rounds(stored_hash, ec); |
| 190 | +
|
| 191 | +if (ec) |
| 192 | + // Invalid hash format |
| 193 | +else if (rounds < 12) |
| 194 | + // Consider re-hashing with higher cost |
| 195 | +---- |
| 196 | + |
| 197 | +== Upgrading Cost Factor |
| 198 | + |
| 199 | +When a user logs in successfully, you can check if their hash needs upgrading: |
| 200 | + |
| 201 | +[source,cpp] |
| 202 | +---- |
| 203 | +system::error_code ec; |
| 204 | +bool valid = bcrypt::compare(password, stored_hash, ec); |
| 205 | +
|
| 206 | +if (valid && !ec) |
| 207 | +{ |
| 208 | + unsigned current_cost = bcrypt::get_rounds(stored_hash, ec); |
| 209 | + |
| 210 | + if (!ec && current_cost < 12) |
| 211 | + { |
| 212 | + // Re-hash with higher cost |
| 213 | + bcrypt::result new_hash = bcrypt::hash(password, 12); |
| 214 | + update_stored_hash(user_id, new_hash.str()); |
| 215 | + } |
| 216 | +} |
| 217 | +---- |
| 218 | + |
| 219 | +== Error Handling |
| 220 | + |
| 221 | +BCrypt defines two error codes: |
| 222 | + |
| 223 | +[cols="1,3"] |
| 224 | +|=== |
| 225 | +| Error | Meaning |
| 226 | + |
| 227 | +| `bcrypt::error::invalid_salt` |
| 228 | +| Salt string is malformed |
| 229 | + |
| 230 | +| `bcrypt::error::invalid_hash` |
| 231 | +| Hash string is malformed |
| 232 | +|=== |
| 233 | + |
| 234 | +These errors indicate either data corruption or malicious input. Log them |
| 235 | +as security events. |
| 236 | + |
| 237 | +== Version Selection |
| 238 | + |
| 239 | +BCrypt has multiple version prefixes: |
| 240 | + |
| 241 | +[cols="1,3"] |
| 242 | +|=== |
| 243 | +| Version | Description |
| 244 | + |
| 245 | +| `version::v2a` |
| 246 | +| Original specification |
| 247 | + |
| 248 | +| `version::v2b` |
| 249 | +| Fixed handling of passwords > 255 chars (recommended) |
| 250 | +|=== |
| 251 | + |
| 252 | +Use `v2b` for new hashes. All versions produce compatible hashes that can |
| 253 | +be verified by any version. |
| 254 | + |
| 255 | +== Security Considerations |
| 256 | + |
| 257 | +**Do:** |
| 258 | + |
| 259 | +* Use cost factor 12 or higher |
| 260 | +* Store the complete hash string (includes salt and cost) |
| 261 | +* Compare in constant time (handled by `compare`) |
| 262 | +* Log invalid hash errors as security events |
| 263 | + |
| 264 | +**Do Not:** |
| 265 | + |
| 266 | +* Store salts separately (they are embedded in the hash) |
| 267 | +* Use bcrypt for general-purpose hashing (use SHA-256) |
| 268 | +* Compare hashes with `==` (timing attacks) |
| 269 | + |
| 270 | +== Summary |
| 271 | + |
| 272 | +[cols="1,3"] |
| 273 | +|=== |
| 274 | +| Function | Purpose |
| 275 | + |
| 276 | +| `bcrypt::hash(password, rounds)` |
| 277 | +| Hash a password with auto-generated salt |
| 278 | + |
| 279 | +| `bcrypt::hash(password, salt, ec)` |
| 280 | +| Hash with explicit salt |
| 281 | + |
| 282 | +| `bcrypt::compare(password, hash, ec)` |
| 283 | +| Verify a password against a hash |
| 284 | + |
| 285 | +| `bcrypt::gen_salt(rounds)` |
| 286 | +| Generate a random salt |
| 287 | + |
| 288 | +| `bcrypt::get_rounds(hash, ec)` |
| 289 | +| Extract cost factor from hash |
| 290 | +|=== |
0 commit comments