More Secure Passwords in Bcrypt — Beating the 72 Bytes Limitation | Monterail (2024)

Table of Contents

  1. What's the Problem with Bcrypt's Security?
  2. So How to Beat the 72 Bytes Limitation?
  3. Summary

Storing passwords is a pretty common workflow that needs to be done when developing web applications, so it's best to use well-established solutions for that.

In Rails, it would be devise which under the hood uses bcrypt for password hashing. And this is a great choice as bcrypt is a secure at least at the time of writing algorithm, and is used very commonly in other technologies. For example, Phoenix on Unix system uses it by default, and I saw it often used in NodeJS projects.

More Secure Passwords in Bcrypt — Beating the 72 Bytes Limitation | Monterail (1)

But it has its limitations, and in this post, I will present you one 72 bytes password length limit. I'll also tell you how to solve this.

What's the Problem with Bcrypt's Security?

So, as mentioned above, most implementations of bcrypt are limited to 72 bytes password length. At first glance, this does not seem so bad after all, 72 bytes makes for quite a long password. But let's check what will happen if we provide a bit longer password to Ruby implementation of bcrypt.

irb(main)> password = "a" * 72 + "someextra"
=> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasomeextra"
irb(main)> hash = BCrypt::Password.create(password)
=> "$2a$12$ijB8LuN33Y6.NoMl3gTLieyd9hYzp7SWD4H6immKiy80DNCTXYgQ."
irb(main)> BCrypt::Password.new(hash) == password
=> true

So our password created what looks like a valid hash, and it is even validated as one. So everything is good, right? Not really. Because if we try to compare that hash to just 72 "a", without the "someextra" part it will still validate:

irb(main)> hash
=> "$2a$12$ijB8LuN33Y6.NoMl3gTLieyd9hYzp7SWD4H6immKiy80DNCTXYgQ."
irb(main)> BCrypt::Password.new(hash) == "a" * 72
=> true

Ok, but as we said before: even without that "someextra" part, as long as the first 72 bytes still create a secure password, it should be fine. But with that, we assume that users create secure passwords, which may not be the case. And if we are using pepper in our setup, a long password can drop it from the hash, reducing its security:

irb(main)> password = "a" * 72 + pepper
=> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaapepper"
irb(main)> hash = BCrypt::Password.create(password)
=> "$2a$12$kMR90psu4jhtp2xb7B35d.36lDuyewZsPEeDRXiVdlrWr9B7CQMVi"
irb(main)> BCrypt::Password.new(hash) == "a" * 72
=> true

What's more, it is important to point out that the limit is 72 bytes, not 72 characters. On the web, we are mostly using UTF-8 (for passwords as well). That means not all characters are 1 byte in size, and one character can take between 1 to 4 bytes.Quick example? Monterail is a Polish company, and in Poland, we have a couple of extra letters, like "ą", which in UTF-8 takes 2 bytes:

irb(main)> "ą".bytesize
=> 2

This means that if we would use only "ą" to create a password, its effective length is reduced to 36 characters:

irb(main)> password = "ą" * 36 + "extra"
=> "ąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąextra"
irb(main)> hash = BCrypt::Password.create(password)
=> "$2a$12$3QyhYPBdqknleoFPiDHn5edo.OF4jgU4D0dYOa/MDncm5EDy0QUB."
irb(main)> BCrypt::Password.new(hash) == "ą" * 36
=> true

To be realistic, in the case of the Polish language, those letters are just something extra on top of the standard Latin alphabet, so in cases of most passwords even if letters like "ą" are used, the effective password length will not be cut in half. But there are alphabets that do not use Latin characters at all and all of them will take more than 1 byte. In extreme cases, the length will be only 24 or maybe even 18 characters, for 3 and 4 bytes accordingly.

And the actual problem here is that it is done silently, no error returned — users don't know that this shortening is happening, even though as shown above, it may silently drop pepper as well.

So How to Beat the 72 Bytes Limitation?

To be fair: this does not make bcrypt insecure — it is a widely used and secure algorithm. It just has a limitation that needs to be taken into consideration when working with it.

There is a great article about password storage on OWASP Cheat Sheet Series, an awesome place to learn more about the problem. And we will use two recommendations from there as solutions to our problem.

One will be to change the algorithm from bcrypt to argon2id, as the password length limit there is 4 GiB, which means: virtually unlimited. So it's important to create some reasonable input limit here, to prevent maliciously long passwords from being provided to create denial attacks.

If argon2 is not an option and you need to stay with bcrypt, the best solution would be to add length validation, to check if a password is not longer than 72 bytes (or even shorter if pepper is used).

Summary

So there you go. The takeaway is this: bcrypt is a secure algorithm but remember that it caps passwords at 72 bytes. You can either check if the passwords are the proper size, or opt to switch to argon2, where you'll have to set a password size limit.

Happy hashing!

More Secure Passwords in Bcrypt — Beating the 72 Bytes Limitation | Monterail (2)

More Secure Passwords in Bcrypt — Beating the 72 Bytes Limitation | Monterail (3)

Rafał Rothenberger

Tags

TechnologyRuby/RailsDevelopment
More Secure Passwords in Bcrypt — Beating the 72 Bytes Limitation | Monterail (2024)

FAQs

Why is bcrypt more secure? ›

Going into more granular detail, bcrypt transforms user passwords into fixed-length strings through a one-way hashing process. This means any hash reversal is virtually inconceivable, and it cannot be changed back to the original password.

What is the password limit for bcrypt? ›

BCrypt hashed passwords and secrets have a 72 character limit.

Is bcrypt still the best? ›

Cybersecurity doesn't have a one-size-fits-all remedy, and despite its strength, bcrypt hashes have been susceptible to exposure in data breaches. Despite this, it remains a standout choice, especially in addressing the critical issues of password reuse and compromised credentials within an organization.

Is bcrypt more secure than SHA256? ›

Compare this to popular hashing algorithms such as MD5 and SHA256, which are designed to hash quickly. They're better for applications that are used frequently and where speed is important, whereas bcrypt is the better option for the safe storage of passwords.

Is bcrypt outdated? ›

Now, bcrypt is very old dated to 1999. It is not a memory-hard password hashing, that is requiring some adjustable huge memory that prevents parallelization like in ASICs that BitCoin miners use. You should rather use modern password encryption function like Argon2.

What are the drawbacks of bcrypt? ›

Another drawback of bcrypt is that it may not be suitable for some applications that require fast or frequent hashing operations, such as API authentication or session management. Bcrypt may also introduce some overhead or latency in your system, especially if you use a high work factor.

What is the better alternative to bcrypt? ›

While there are of course deeper nuances to Argon2, bcrypt, and scrypt, the choice between them boils down to weighing computing and time requirements against memory hardness and parameter number. Argon2 is a great memory-hard password hashing algorithm, which makes it good for offline key derivation.

Is bcrypt vulnerable? ›

bcrypt is an A library to help you hash passwords. Affected versions of this package are vulnerable to Insecure Encryption. Data is truncated wrong when its length is greater than 255 bytes.

What is the most secure encryption algorithm in use today? ›

AES 256-bit encryption is the strongest and most robust encryption standard that is commercially available today. While it is theoretically true that AES 256-bit encryption is harder to crack than AES 128-bit encryption, AES 128-bit encryption has never been cracked.

What is the most secure password algorithm? ›

To protect passwords, experts suggest using a strong and slow hashing algorithm like Argon2 or Bcrypt, combined with salt (or even better, with salt and pepper).

Why is SHA-256 not good for passwords? ›

SHA-256 is the successor of SHA-1, a widely popular algorithm in the past. However, it has since been deemed insecure due to vulnerabilities discovered in its code.

Which is more secure MD5 or bcrypt? ›

Data security systems that rely on MD5 can be easily hacked by anyone with a basic understanding of the function. On the other hand, bcrypt is not broken. As a result, it's still able to keep passwords and information safe. Modern passwords should never be stored behind MD5.

Why is bcrypt better than MD5? ›

Data security systems that rely on MD5 can be easily hacked by anyone with a basic understanding of the function. On the other hand, bcrypt is not broken. As a result, it's still able to keep passwords and information safe. Modern passwords should never be stored behind MD5.

Which hashing function is most secure? ›

Common attacks like brute force attacks can take years or even decades to crack the hash digest, so SHA-2 is considered the most secure hash algorithm.

What advantage does the bcrypt hashing utility have over many others? ›

Bcrypt is more resistant to simple attacks like brute force compared to algorithms such as SHA-256 and MD5 . Bcrypt uses random salts, which prevent the creation of lookup tables and enhance security . It also has a cost factor and salt value, further increasing its resistance to brute force attacks .

Which encryption algorithm is most secure? ›

AES-256 encryption is extremely secure. It is the most secure encryption algorithm available today and is used extensively in government and military applications, as well as by businesses operating in highly regulated industries.

Top Articles
How to Use a Pell Grant Calculator
Help! I got ghosted by HR 'after going through a million rounds' of job interviews
$4,500,000 - 645 Matanzas CT, Fort Myers Beach, FL, 33931, William Raveis Real Estate, Mortgage, and Insurance
Maxtrack Live
Pollen Count Los Altos
Global Foods Trading GmbH, Biebesheim a. Rhein
Splunk Stats Count By Hour
Celebrity Extra
Jonathon Kinchen Net Worth
Pitt Authorized User
Konkurrenz für Kioske: 7-Eleven will Minisupermärkte in Deutschland etablieren
Sportsman Warehouse Cda
7.2: Introduction to the Endocrine System
P2P4U Net Soccer
Mndot Road Closures
Delectable Birthday Dyes
Phillies Espn Schedule
Craigslist Chautauqua Ny
Hmr Properties
Nj Scratch Off Remaining Prizes
Rainfall Map Oklahoma
Craigslist Free Stuff Santa Cruz
Jenn Pellegrino Photos
Hellraiser III [1996] [R] - 5.8.6 | Parents' Guide & Review | Kids-In-Mind.com
Lonesome Valley Barber
No Hard Feelings - Stream: Jetzt Film online anschauen
Strange World Showtimes Near Roxy Stadium 14
Gayla Glenn Harris County Texas Update
Eine Band wie ein Baum
Pickswise Review 2024: Is Pickswise a Trusted Tipster?
Azpeople View Paycheck/W2
Melissababy
Ahn Waterworks Urgent Care
The Many Faces of the Craigslist Killer
How To Tighten Lug Nuts Properly (Torque Specs) | TireGrades
Mandy Rose - WWE News, Rumors, & Updates
Encore Atlanta Cheer Competition
Jesus Calling Feb 13
Sam's Club Near Wisconsin Dells
Why Are The French So Google Feud Answers
Rocksteady Steakhouse Menu
Iban's staff
Midsouthshooters Supply
Uvalde Topic
Karen Wilson Facebook
Divinity: Original Sin II - How to Use the Conjurer Class
Black Adam Showtimes Near Kerasotes Showplace 14
Fredatmcd.read.inkling.com
Helpers Needed At Once Bug Fables
O'reilly's Eastman Georgia
Www.card-Data.com/Comerica Prepaid Balance
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 6144

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.