What is bcrypt?
Bcrypt is a cryptographic hash function designed for password hashing and safe storing in the backend of applications in a way that is less susceptible to dictionary-based cyberattacks. It was created in 1999 by Niels Provos and David Mazières, using the Blowfish cipher algorithm as its base.
Bcrypt runs a complex hashing process, during which a user’s password is transformed into a fixed-length thread of characters. It uses a one-way hash function, meaning that once the password is hashed, it cannot be reversed to its original form. Every time the user logs into their account, bcrypt hashes their password anew and compares the new hash value to the version stored in the system’s memory to check if the passwords match.
Instead of simply hashing the given password, bcrypt adds a random piece of data, called salt, to create a unique hash that is almost impossible to break with automated guesses during hash dictionary and brute force attacks.
Bcrypt also stands out among other hashing algorithms because it uses a cost factor. With it, you can determine the number of password iterations and hashing rounds to be performed, increasing the amount of time, effort, and computational resources needed to calculate the final hash value. The cost factor makes bcrypt a slow algorithm that takes significantly more time to produce a hash key, turning it into a safe password-storing tool.
How does bcrypt work?
To hash passwords, bcrypt’s password hashing algorithm combines the password string, salt, and the cost to derive a 24-byte hash using base 64 encoding.
Let’s take a closer look at each component of the hash:
- Password string. A strong password is necessary for deriving an impenetrable hash. This is why bcrypt introduced a password-strengthening function. If the plain text password is too short, it can be stretched to become longer and more complex. Bcrypt allows up to 72 bytes for a password, though generally, up to 56 bytes are used to generate a 31-character hash. The password hash can make up to 23 bytes of the calculated 24-byte hash.
- Salt. A random 16-byte salt value is added in front of the plain text password. The salt is then hashed, resulting in a 22-character string that is placed in front of the password hash.
- Cost factor. A numeric cost is added in front of the salt and the password hashes, showing how many password iterations were made before the hash was generated.
The hash string is then prefixed with bcrypt’s hash algorithm identifiers: $2a$, $2y$, or $2b$.
Why you should use bcrypt to hash stored passwords
Bcrypt is a valuable tool to use to hash and store passwords. Its major benefits include:
- Slow runtime. Bcrypt is a slow-functioning algorithm that takes time to create password hashes and requires time to decrypt them, significantly slowing hacker attempts to break the bcrypt hash. Because it takes longer for a threat actor to act, security software or a user can notice suspicious activity and prevent hackers from breaching sensitive data.
- Usage of salt. Adding a random piece of data and hashing it with the password helps generate unique password hashes resistant to rainbow table attacks. Salting passwords guarantees the highest safety standards of password storekeeping.
- Adapts to changes. Bcrypt is an adaptive tool that can evolve with hardware and software optimization. The safety of the hashed password depends on how fast its hash can be calculated. Computers becoming more powerful means that hackers can be quicker in hashing passwords. However, bcrypt uses a variable number of password iterations, which can significantly increase computational efforts. So while computers become faster, bcrypt slows down the hashing process, bringing threat actors to the same halt they would experience with slower and older techniques.
Bcrypt vs. SHA256
Bcrypt and SHA256 are two of the most popular cryptographic hashing algorithms to use. However, it’s important to determine your needs before deciding which one is right for you. Let’s look at what SHA256 is and how it compares to bcrypt.
The name SHA256 stands for “secure hash algorithm” with a 256-bit key. SHA 256 produces a one-way hash function, turning input of any length into a 256-bit hash value output. It’s designed in such a way that the same input will always produce the same hash value. SHA256 is computationally efficient and is widely used for data integrity checks, digital signatures, and other cryptographic applications.
The main difference between bcrypt and SHA256 is that bcrypt is created to calculate the hash as slowly as possible without hindering users, whereas SHA256 is designed to be computationally fast. Another important detail is that SHA256 hashes don’t include the salt element, which makes them more susceptible to dictionary-based cyberattacks.
So while SHA256 is more suitable for applications that require frequent interaction, bcrypt is a better solution for safely storing passwords.
|Slow hash||Fast hash|
|Good for safekeeping passwords||Good for frequently used cryptographic applications|
Is bcrypt safe for password hashing?
Bcrypt is a safe cryptographic algorithm for hashing and storing passwords. Thanks to the added salt and cost factors, bcrypt provides robust protection against brute force and rainbow table attacks. Hackers are unlikely to guess the password hash because of bcrypt’s slow-working hashing algorithm, which requires extreme computational effort.
Bcrypt’s flexible cryptographic setup is also designed to make the hashing algorithm increasingly harder to solve and to follow the steps of the new, faster technology that hackers may use.
However, it’s important to note that bcrypt may be illegal or bound to specific laws and regulations in a particular country or region. Because of that, it is always recommended to consult with legal experts before using bcrypt.