Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4133 pages
Node.js, PHP and APIs

Hashing even a short password with passlib fails after bcrypt 5.0 is installed

Your error

ValueError: password cannot be longer than 72 bytes, truncate manually if necessary (e.g. my_password[:72])2 more lines it can show

Fix it fast

Most common cause, 1 of 3

bcrypt 5 refuses the long test string passlib hashes at startup

Pin bcrypt below 5 in your requirements so every environment resolves the same version.

Fix
# requirements.txt
passlib==1.7.4
bcrypt==4.3.0

pip install -r requirements.txt
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

bcrypt 5.0.0 raises ValueError for any input over 72 bytes instead of truncating it. passlib 1.7.4, last released in 2020, hashes a long test string when it loads the bcrypt backend, so the first hash call fails even for short passwords, often only on a fresh deploy. Pin bcrypt to 4.3.0, or call the bcrypt library directly; existing hashes keep working.

  • Applies to passlib 1.7.4, bcrypt 5.0.0, bcrypt 4.3.0, Python 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the installed versions in the environment that fails. A deploy that resolves bcrypt freshly often gets 5.x while your laptop still has 4.x.

Run this first
python -m pip show passlib bcrypt | grep -E "^(Name|Version)"

Then find the line you see in this list and open its fix:

  1. 1bcrypt 5 refuses the long test string passlib hashes at startupThe traceback goes through passlib/handlers/bcrypt.py into _bcrypt.hashpw, and the password you passed is short
  2. 2passlib itself is no longer maintainedPyPI shows passlib 1.7.4, released 8 October 2020, as the latest version
  3. 3Real user passwords can exceed 72 bytesAfter removing passlib, the same ValueError appears for some users only, usually with long passphrases or many multi-byte characters

What does the error look like?

  • ValueError: password cannot be longer than 72 bytes, truncate manually if necessary (e.g. my_password[:72])
  • (trapped) error reading bcrypt version
  • AttributeError: module 'bcrypt' has no attribute '__about__'

Cause 1 of 3

bcrypt 5 refuses the long test string passlib hashes at startup

You seeThe traceback goes through passlib/handlers/bcrypt.py into _bcrypt.hashpw, and the password you passed is short

Check

The bcrypt changelog for 5.0.0 says hashpw now raises ValueError above 72 bytes where it used to truncate silently. passlib's backend loader still assumes the old behaviour, so the error comes from passlib's own check, not from your input.

Check
python -c "from passlib.context import CryptContext; print(CryptContext(schemes=['bcrypt']).hash('pw')[:4])"

Fix

Pin bcrypt below 5 in your requirements so every environment resolves the same version.

Fix
# requirements.txt
passlib==1.7.4
bcrypt==4.3.0

pip install -r requirements.txt

Verify

The same one-liner prints $2b$ and verify() returns True. The '(trapped) error reading bcrypt version' lines may still print; they are logged by passlib and do not stop hashing.

Verify
python -c "from passlib.context import CryptContext; c=CryptContext(schemes=['bcrypt']); h=c.hash('pw'); print(c.verify('pw', h))"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

passlib itself is no longer maintained

You seePyPI shows passlib 1.7.4, released 8 October 2020, as the latest version

Check

Pinning bcrypt keeps an old dependency alive. If passlib is only used for bcrypt, the bcrypt package covers hashing and checking on its own, and the hashes passlib stored use the standard $2b$ format that bcrypt reads.

Check
grep -rn "passlib" --include=*.py . | grep -v venv

Fix

Replace the passlib calls with two small functions. Existing stored hashes verify without any migration.

Fix
import bcrypt

def hash_password(password: str) -> str:
    return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()

def verify_password(password: str, hashed: str) -> bool:
    return bcrypt.checkpw(password.encode(), hashed.encode())

Verify

Log in with an account created before the change; checkpw accepts its old passlib hash.

Verify
python -c "import bcrypt; print(bcrypt.checkpw(b'pw', b'<an existing hash from your users table>'))"

Confirmed by pypi.org

Did this fix it?

Cause 3 of 3

Real user passwords can exceed 72 bytes

You seeAfter removing passlib, the same ValueError appears for some users only, usually with long passphrases or many multi-byte characters

Check

72 is a byte count, not a character count, so a 30-character password in a non-Latin script can pass it. bcrypt 5 no longer truncates for you.

Check
python -c "print(len('pässwörd'.encode()))"

Fix

Follow the bcrypt README: pre-hash with SHA-256 and base64-encode the digest before bcrypt, or enforce a maximum byte length at sign-up. Pre-hashing changes the stored format, so use it for new hashes and keep checkpw for old ones until users log in again.

Fix
import base64, hashlib, bcrypt

def prehash(password: str) -> bytes:
    return base64.b64encode(hashlib.sha256(password.encode()).digest())

hashed = bcrypt.hashpw(prehash(password), bcrypt.gensalt())

Verify

A 200-character password hashes and verifies with the same prehash function.

Verify
python -c "import base64,hashlib,bcrypt; p=base64.b64encode(hashlib.sha256(('x'*200).encode()).digest()); print(bcrypt.checkpw(p, bcrypt.hashpw(p, bcrypt.gensalt())))"

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Pin security libraries to exact versions and update them on purpose, with a test that hashes and verifies a password.
  • Lock dependencies (pip-tools, uv or Poetry lock files) so production installs the versions you tested.
  • Keep password hashing in one module so a library swap touches one file.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.