What column type/length should I use for storing a Bcrypt hashed password in a Database?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When designing a system that involves user authentication, one important aspect is the storage of hashed passwords. The Bcrypt hashing algorithm is widely recommended for password hashing due to its adaptive nature and resistance to brute-force attacks. However, a crucial question arises: what column type and length should be used to store these Bcrypt hashed passwords in a database?
Understanding Bcrypt
Bcrypt is a sophisticated and time-tested hashing algorithm. It offers several advantages:
- Salt Generation: Automatically integrates a unique salt for each password, enhancing security.
- Adaptive Hashing: Allows adjustment of its computational cost (aka work factor), keeping pace with advancing hardware capabilities.
- Fixed-Length Output: Regardless of the input length, Bcrypt produces a fixed-length hash.
Bcrypt generates a 60-character string (including parts like the algorithm version, cost factor, and salt value). It can extend up to 72 bytes if the input string uses 8-bit characters. This fixed length is crucial when considering the column specifications in your database.
Choosing the Right Column Type
Storage Requirements
When choosing a column type for storing Bcrypt hashed passwords, you should consider the following requirements:
- Character Storage: Since Bcrypt outputs a text-based string, a character-based column type is required.
- Consistent Length: Bcrypt provides a consistently sized output, which simplifies storage decisions.
Recommended Column Types
In SQL Databases
In typical SQL databases like MySQL or PostgreSQL, the following column types are suitable:
- VARCHAR(60): This is the simplest option, given Bcrypt's fixed 60-character length. It's both efficient and straightforward since this type is specifically optimized for variable-length strings.
- CHAR(60): If your database frequently queries the hashed password field, consider using
CHAR(60). This can sometimes improve performance because it employs fixed-length storage, though the benefits are generally marginal.
In NoSQL Databases
For NoSQL databases, such as MongoDB, character string storage is inherently flexible:
- String: Utilize a simple string data type, which naturally accommodates Bcrypt's fixed output size.
Additional Considerations
Cost Factor
The cost factor (or work factor) determines the computational complexity of the Bcrypt algorithm. Higher values increase security but also require more processing time. It's important to note that the cost factor does not affect the hash length; hence, it does not influence column size.
Future-Proofing
Although Bcrypt hashes are currently limited to 60 characters, considering slight updates to the algorithm in the future could mean minor increases in size. As such, opting for a VARCHAR(255) or similar type might facilitate future expansions without the need for a structural change.
Bcrypt Column Storage Summary
| Column Type | Database System | Length | Characteristics |
| VARCHAR(60) | SQL Databases | 60 | Dynamic, efficient storage |
| CHAR(60) | SQL Databases | 60 | Fixed, potentially faster |
| String | NoSQL Databases | N/A | Flexible, straightforward |
Using Bcrypt to hash passwords is a vital part of securing your application's user data. Selecting an appropriate database column type and length ensures that your system is both secure and efficient. With consistent handling of hashed password sizes, you can focus more on other security practices, knowing your password infrastructure is solid.

