Skip to main content

bcrypt

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

Good 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, headers for Python (if you’re not using pypy), and headers for the libffi libraries available on your system.

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

$ sudo apt-get install build-essential libffi-dev python-dev

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

$ sudo yum install gcc libffi-devel python-devel

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

$ apk add --update musl-dev gcc libffi-dev

Alternatives

While bcrypt remains a good 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

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.

Release files for bcrypt 3.2.1

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 3.2.1
File Size Uploaded
bcrypt-3.2.1.tar.gz 42.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for bcrypt 3.2.1
File
bcrypt-3.2.1-cp36-abi3-win_amd64.whl CPython 3.6 abi3 Windows x86-64 Details
bcrypt-3.2.1-cp36-abi3-win32.whl CPython 3.6 abi3 Windows x86-32 Details
bcrypt-3.2.1-cp36-abi3-musllinux_1_1_x86_64.whl CPython 3.6 abi3 Linux musl 1.1+ x86-64 Details
bcrypt-3.2.1-cp36-abi3-musllinux_1_1_aarch64.whl CPython 3.6 abi3 Linux musl 1.1+ ARM64 Details
bcrypt-3.2.1-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.6 abi3 Linux glibc 2.17+ x86-64 Details
bcrypt-3.2.1-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl CPython 3.6 abi3 Linux glibc 2.17+ x86-64, Linux glibc 2.24+ x86-64 Details
bcrypt-3.2.1-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.6 abi3 Linux glibc 2.17+ ARM64 Details
bcrypt-3.2.1-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-3.2.1-cp36-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.6 abi3 Linux glibc 2.12+ x86-64, Linux glibc 2.5+ x86-64 Details
bcrypt-3.2.1-cp36-abi3-macosx_10_10_universal2.whl CPython 3.6 abi3 macOS 10.10+ universal2 (ARM64, x86-64) Details

Total release size: 562.4 kB

Release files / bcrypt-3.2.1.tar.gz

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

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

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

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

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

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

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

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

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

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

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

Release files / bcrypt-3.2.1-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl

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

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

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

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

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

Release files / bcrypt-3.2.1-cp36-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL bcrypt-3.2.1-cp36-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 64.3 kB
Tags CPython 3.6 Linux glibc 2.12+ x86-64 Linux glibc 2.5+ x86-64 abi3
SHA-256 checksum
How to use checksums
e51bbe6d2f1438d00a757ab94f5626b5e25c25dc8c6b818119a0add7dcdb152c
BLAKE2b-256 checksum
How to use checksums
4e2d8cfdeef72810d2a2f457900a14c5c6347b3323b66f97400420f4123a867d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.1

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

Download URL bcrypt-3.2.1-cp36-abi3-macosx_10_10_universal2.whl
Size 49.7 kB
Tags CPython 3.6 abi3 macOS 10.10+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
0fa94ff90b070d552add1e0f3380e8e0e52d8aecff34e39724c8edee700c23bd
BLAKE2b-256 checksum
How to use checksums
a7063b5a48ede13cdaf1dca9af056cfa9bdf5cab50cbb19b32607b9e933cfe55
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

4.0.0

12 release files

This release

3.2.1 This release

11 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