Using Bcrypt as password encoder can be problematic when passwords are non-ASCII characters
It’s an issue to simply use the Spring Security BCryp function as password encoder. What is Bcrypt? https://www.baeldung.com/spring-security-registration-password-encoding-bcrypt#define-the-password-encoder
Bcrypt has limit to hash the password which is maximum 72 bytes.
It works fine for ASCII characters if restricting the input to be less than 72 ASCII characters, but it doesn’t work well for non-ASCII. If your passwords are longer than 72 bytes, the remaing parts are ignored.
Example: password ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ is 36 charaters and 72 bytes.
using password ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄaaaa
can also login. Bcrypt sees the above two passwords are the same and that means the password is restricted to 72 bytes, not by the number of characters.
You might think why not to restrict the password to ASCII characters. It’s not recommended by all guidelines because it decreases the complexity of password. Then why password length should be restricted? DDoS Attack!
All password guideliens suggest to not limit the type of characters
Verify that there are no password composition rules limiting the type of characters permitted. There should be no requirement for upper or lower case or numbers or special characters.
ASVS/0x11-V2-Authentication.md at master · OWASP/ASVS
Verifiers SHALL require subscriber-chosen memorized secrets to be at least 8 characters in length. Verifiers SHOULD permit subscriber-chosen memorized secrets at least 64 characters in length.
The solution for the problem is not new, and in 2016 dropbox published the solution: https://dropbox.tech/security/how-dropbox-securely-stores-your-passwords
instead of
bcrypt(passwrod)
you use
bcrypt(sha512(password))
As sha512(password) will ouput the constant length that bcrypt accepts, this can help the password encoder to support non-ASCII characters.
How safe is this approach? it’s verifed in https://security.stackexchange.com/questions/6623/pre-hash-password-before-applying-bcrypt-to-avoid-restricting-password-length