Skip to main content

bcrypt

Latest Version https://github.com/pyca/bcrypt/workflows/CI/badge.svg?branch=main

Acceptable password hashing for your software and your servers

Installation

To install bcrypt, simply:

$ pip install bcrypt

Note that bcrypt should build very easily on Linux provided you have a C compiler and a Rust compiler (the minimum supported Rust version is 1.56.0).

For Debian and Ubuntu, the following command will ensure that the required dependencies are installed:

$ sudo apt-get install build-essential cargo

For Fedora and RHEL-derivatives, the following command will ensure that the required dependencies are installed:

$ sudo yum install gcc cargo

For Alpine, the following command will ensure that the required dependencies are installed:

$ apk add --update musl-dev gcc cargo

Alternatives

While bcrypt remains an acceptable choice for password storage, depending on your specific use case you may also want to consider using scrypt (either via standard library or cryptography) or argon2id via argon2_cffi.

Changelog

4.0.0

  • bcrypt is now implemented in Rust. Users building from source will need to have a Rust compiler available. Nothing will change for users downloading wheels.

  • We no longer ship manylinux2010 wheels. Users should upgrade to the latest pip to ensure this doesn’t cause issues downloading wheels on their platform. We now ship manylinux_2_28 wheels for users on new enough platforms.

  • NUL bytes are now allowed in inputs.

3.2.2

  • Fixed packaging of py.typed files in wheels so that mypy works.

3.2.1

  • Added support for compilation on z/OS

  • The next release of bcrypt with be 4.0 and it will require Rust at compile time, for users building from source. There will be no additional requirement for users who are installing from wheels. Users on most platforms will be able to obtain a wheel by making sure they have an up to date pip. The minimum supported Rust version will be 1.56.0.

  • This will be the final release for which we ship manylinux2010 wheels. Going forward the minimum supported manylinux ABI for our wheels will be manylinux2014. The vast majority of users will continue to receive manylinux wheels provided they have an up to date pip.

3.2.0

  • Added typehints for library functions.

  • Dropped support for Python versions less than 3.6 (2.7, 3.4, 3.5).

  • Shipped abi3 Windows wheels (requires pip >= 20).

3.1.7

  • Set a setuptools lower bound for PEP517 wheel building.

  • We no longer distribute 32-bit manylinux1 wheels. Continuing to produce them was a maintenance burden.

3.1.6

  • Added support for compilation on Haiku.

3.1.5

  • Added support for compilation on AIX.

  • Dropped Python 2.6 and 3.3 support.

  • Switched to using abi3 wheels for Python 3. If you are not getting a wheel on a compatible platform please upgrade your pip version.

3.1.4

  • Fixed compilation with mingw and on illumos.

3.1.3

  • Fixed a compilation issue on Solaris.

  • Added a warning when using too few rounds with kdf.

3.1.2

  • Fixed a compile issue affecting big endian platforms.

  • Fixed invalid escape sequence warnings on Python 3.6.

  • Fixed building in non-UTF8 environments on Python 2.

3.1.1

  • Resolved a UserWarning when used with cffi 1.8.3.

3.1.0

  • Added support for checkpw, a convenience method for verifying a password.

  • Ensure that you get a $2y$ hash when you input a $2y$ salt.

  • Fixed a regression where $2a hashes were vulnerable to a wraparound bug.

  • Fixed compilation under Alpine Linux.

3.0.0

  • Switched the C backend to code obtained from the OpenBSD project rather than openwall.

  • Added support for bcrypt_pbkdf via the kdf function.

2.0.0

  • Added support for an adjustible prefix when calling gensalt.

  • Switched to CFFI 1.0+

Usage

Password Hashing

Hashing and then later checking that a password matches the previous hashed password is very simple:

>>> import bcrypt
>>> password = b"super secret password"
>>> # Hash a password for the first time, with a randomly-generated salt
>>> hashed = bcrypt.hashpw(password, bcrypt.gensalt())
>>> # Check that an unhashed password matches one that has previously been
>>> # hashed
>>> if bcrypt.checkpw(password, hashed):
...     print("It Matches!")
... else:
...     print("It Does not Match :(")

KDF

As of 3.0.0 bcrypt now offers a kdf function which does bcrypt_pbkdf. This KDF is used in OpenSSH’s newer encrypted private key format.

>>> import bcrypt
>>> key = bcrypt.kdf(
...     password=b'password',
...     salt=b'salt',
...     desired_key_bytes=32,
...     rounds=100)

Adjustable Work Factor

One of bcrypt’s features is an adjustable logarithmic work factor. To adjust the work factor merely pass the desired number of rounds to bcrypt.gensalt(rounds=12) which defaults to 12):

>>> import bcrypt
>>> password = b"super secret password"
>>> # Hash a password for the first time, with a certain number of rounds
>>> hashed = bcrypt.hashpw(password, bcrypt.gensalt(14))
>>> # Check that a unhashed password matches one that has previously been
>>> #   hashed
>>> if bcrypt.checkpw(password, hashed):
...     print("It Matches!")
... else:
...     print("It Does not Match :(")

Adjustable Prefix

Another one of bcrypt’s features is an adjustable prefix to let you define what libraries you’ll remain compatible with. To adjust this, pass either 2a or 2b (the default) to bcrypt.gensalt(prefix=b"2b") as a bytes object.

As of 3.0.0 the $2y$ prefix is still supported in hashpw but deprecated.

Maximum Password Length

The bcrypt algorithm only handles passwords up to 72 characters, any characters beyond that are ignored. To work around this, a common approach is to hash a password with a cryptographic hash (such as sha256) and then base64 encode it to prevent NULL byte problems before hashing the result with bcrypt:

>>> password = b"an incredibly long password" * 10
>>> hashed = bcrypt.hashpw(
...     base64.b64encode(hashlib.sha256(password).digest()),
...     bcrypt.gensalt()
... )

Compatibility

This library should be compatible with py-bcrypt and it will run on Python 3.6+, and PyPy 3.

C Code

This library uses code from OpenBSD.

Security

bcrypt follows the same security policy as cryptography, if you identify a vulnerability, we ask you to contact us privately.

Metadata

Release files for bcrypt 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bcrypt 4.0.0
File Size Uploaded
bcrypt-4.0.0.tar.gz 25.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for bcrypt 4.0.0
File
bcrypt-4.0.0-cp36-abi3-win_amd64.whl CPython 3.6 abi3 Windows x86-64 Details
bcrypt-4.0.0-cp36-abi3-win32.whl CPython 3.6 abi3 Windows x86-32 Details
bcrypt-4.0.0-cp36-abi3-musllinux_1_1_x86_64.whl CPython 3.6 abi3 Linux musl 1.1+ x86-64 Details
bcrypt-4.0.0-cp36-abi3-musllinux_1_1_aarch64.whl CPython 3.6 abi3 Linux musl 1.1+ ARM64 Details
bcrypt-4.0.0-cp36-abi3-manylinux_2_28_x86_64.whl CPython 3.6 abi3 Linux glibc 2.28+ x86-64 Details
bcrypt-4.0.0-cp36-abi3-manylinux_2_28_aarch64.whl CPython 3.6 abi3 Linux glibc 2.28+ ARM64 Details
bcrypt-4.0.0-cp36-abi3-manylinux_2_24_x86_64.whl CPython 3.6 abi3 Linux glibc 2.24+ x86-64 Details
bcrypt-4.0.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.6 abi3 Linux glibc 2.17+ x86-64 Details
bcrypt-4.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.6 abi3 Linux glibc 2.17+ ARM64 Details
bcrypt-4.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_24_aarch64.whl CPython 3.6 abi3 Linux glibc 2.17+ ARM64, Linux glibc 2.24+ ARM64 Details
bcrypt-4.0.0-cp36-abi3-macosx_10_10_universal2.whl CPython 3.6 abi3 macOS 10.10+ universal2 (ARM64, x86-64) Details

Total release size: 5.6 MB

Release files / bcrypt-4.0.0.tar.gz

Download URL bcrypt-4.0.0.tar.gz
Size 25.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c59c170fc9225faad04dde1ba61d85b413946e8ce2e5f5f5ff30dfd67283f319
BLAKE2b-256 checksum
How to use checksums
99f2b71b9b5b2400fffac7d42c560ac89f302c4d8e328337b2f05f0a4d9e590d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-win_amd64.whl

Download URL bcrypt-4.0.0-cp36-abi3-win_amd64.whl
Size 153.1 kB
Tags CPython 3.6 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
0b0f0c7141622a31e9734b7f649451147c04ebb5122327ac0bd23744df84be90
BLAKE2b-256 checksum
How to use checksums
b1f98c8e387e1c560448ada45266b6457be8949e58a0b5cb59e36d7506546139
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-win32.whl

Download URL bcrypt-4.0.0-cp36-abi3-win32.whl
Size 159.9 kB
Tags CPython 3.6 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
dc6ec3dc19b1c193b2f7cf279d3e32e7caf447532fbcb7af0906fe4398900c33
BLAKE2b-256 checksum
How to use checksums
b346fe43bd0912f884c91532e7b3f30923bf43861509b1796f38e1ff841b9653
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-musllinux_1_1_x86_64.whl

Download URL bcrypt-4.0.0-cp36-abi3-musllinux_1_1_x86_64.whl
Size 624.6 kB
Tags CPython 3.6 Linux musl 1.1+ x86-64 abi3
SHA-256 checksum
How to use checksums
ede0f506554571c8eda80db22b83c139303ec6b595b8f60c4c8157bdd0bdee36
BLAKE2b-256 checksum
How to use checksums
f4544bf31e9b77b1c64b3d45cd04d42dbac6b31e9599174d5429238a7a5913d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-musllinux_1_1_aarch64.whl

Download URL bcrypt-4.0.0-cp36-abi3-musllinux_1_1_aarch64.whl
Size 614.6 kB
Tags CPython 3.6 Linux musl 1.1+ ARM64 abi3
SHA-256 checksum
How to use checksums
bf413f2a9b0a2950fc750998899013f2e718d20fa4a58b85ca50b6df5ed1bbf9
BLAKE2b-256 checksum
How to use checksums
3ac4085d1cbe08f2ac4933db949d5ad9d6d0210e451948e06f247857ab97c1f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-manylinux_2_28_x86_64.whl

Download URL bcrypt-4.0.0-cp36-abi3-manylinux_2_28_x86_64.whl
Size 594.4 kB
Tags CPython 3.6 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
2d0dd19aad87e4ab882ef1d12df505f4c52b28b69666ce83c528f42c07379227
BLAKE2b-256 checksum
How to use checksums
95d1cdfcf698433b03af76df3f1ee15c840b1f6e54dcdd5a390a6d581d512ec4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-manylinux_2_28_aarch64.whl

Download URL bcrypt-4.0.0-cp36-abi3-manylinux_2_28_aarch64.whl
Size 584.8 kB
Tags CPython 3.6 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
594780b364fb45f2634c46ec8d3e61c1c0f1811c4f2da60e8eb15594ecbf93ed
BLAKE2b-256 checksum
How to use checksums
7ed973369b68fba1a4c1c6977a98466af2872b3b81ec3de341cbd8222825ee8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-manylinux_2_24_x86_64.whl

Download URL bcrypt-4.0.0-cp36-abi3-manylinux_2_24_x86_64.whl
Size 594.0 kB
Tags CPython 3.6 Linux glibc 2.24+ x86-64 abi3
SHA-256 checksum
How to use checksums
7c7dd6c1f05bf89e65261d97ac3a6520f34c2acb369afb57e3ea4449be6ff8fd
BLAKE2b-256 checksum
How to use checksums
19c1c808dc7bacc620fefa7e7f573fc6b26b5b3030afe5fea7b16e8d87b4be44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL bcrypt-4.0.0-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 594.1 kB
Tags CPython 3.6 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
bfb67f6a6c72dfb0a02f3df51550aa1862708e55128b22543e2b42c74f3620d7
BLAKE2b-256 checksum
How to use checksums
c57714bbcd08ad265577ad6ea8e8980b9c0ad668cecfd241ae169b6747c4491b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL bcrypt-4.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 584.3 kB
Tags CPython 3.6 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
1c3334446fac200499e8bc04a530ce3cf0b3d7151e0e4ac5c0dddd3d95e97843
BLAKE2b-256 checksum
How to use checksums
54e564354519c6e6aee70994b9a948b7e823b4012bc7f08a614456c3517560a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_24_aarch64.whl

Download URL bcrypt-4.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.manylinux_2_24_aarch64.whl
Size 584.3 kB
Tags CPython 3.6 Linux glibc 2.17+ ARM64 Linux glibc 2.24+ ARM64 abi3
SHA-256 checksum
How to use checksums
8780e69f9deec9d60f947b169507d2c9816e4f11548f1f7ebee2af38b9b22ae4
BLAKE2b-256 checksum
How to use checksums
ddbe70eee1a2a62b1986e9a60f74b0d7e095bc50fd7cd67109fd82377fa22b90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release files / bcrypt-4.0.0-cp36-abi3-macosx_10_10_universal2.whl

Download URL bcrypt-4.0.0-cp36-abi3-macosx_10_10_universal2.whl
Size 475.0 kB
Tags CPython 3.6 abi3 macOS 10.10+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
845b1daf4df2dd94d2fdbc9454953ca9dd0e12970a0bfc9f3dcc6faea3fa96e4
BLAKE2b-256 checksum
How to use checksums
877bb9bda96f6fc870d1125f8c259bf905510f526b9dbeed4ef2a0df6e0106dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

Release history Release notifications | RSS feed

5.0.0

63 release files

4.3.0

51 release files

4.2.1

25 release files

4.2.0

27 release files

4.1.2

27 release files

4.1.1

17 release files

4.1.0

16 release files

This release

4.0.0 This release

12 release files

3.2.0

10 release files

3.1.7

19 release files

3.1.6

19 release files

3.1.5

19 release files

3.1.4

34 release files

3.1.3

36 release files

3.1.2

36 release files

3.1.1

31 release files

3.1.0

31 release files

3.0.0

30 release files

2.0.0

13 release files

1.1.1

11 release files

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page