Debian Codegraph @

README.rst

raw
1 bcrypt
2 ======
3
4 .. image:: https://img.shields.io/pypi/v/bcrypt.svg
5 :target: https://pypi.org/project/bcrypt/
6 :alt: Latest Version
7
8 .. image:: https://github.com/pyca/bcrypt/workflows/CI/badge.svg?branch=main
9 :target: https://github.com/pyca/bcrypt/actions?query=workflow%3ACI+branch%3Amain
10
11 Acceptable password hashing for your software and your servers (but you should
12 really use argon2id or scrypt)
13
14
15 Installation
16 ============
17
18 To install bcrypt, simply:
19
20 .. code:: console
21
22 $ pip install bcrypt
23
24 Note that bcrypt should build very easily on Linux provided you have a C
25 compiler and a Rust compiler (the minimum supported Rust version is 1.56.0).
26
27 For Debian and Ubuntu, the following command will ensure that the required dependencies are installed:
28
29 .. code:: console
30
31 $ sudo apt-get install build-essential cargo
32
33 For Fedora and RHEL-derivatives, the following command will ensure that the required dependencies are installed:
34
35 .. code:: console
36
37 $ sudo yum install gcc cargo
38
39 For Alpine, the following command will ensure that the required dependencies are installed:
40
41 .. code:: console
42
43 $ apk add --update musl-dev gcc cargo
44
45
46 Alternatives
47 ============
48
49 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`_.
50
51 Changelog
52 =========
53
54 5.0.0
55 -----
56
57 * Bumped MSRV to 1.74.
58 * Added support for Python 3.14 and free-threaded Python 3.14.
59 * Added support for Windows on ARM.
60 * Passing ``hashpw`` a password longer than 72 bytes now raises a
61 ``ValueError``. Previously the password was silently truncated, following the
62 behavior of the original OpenBSD ``bcrypt`` implementation.
63
64 4.3.0
65 -----
66
67 * Dropped support for Python 3.7.
68 * We now support free-threaded Python 3.13.
69 * We now support PyPy 3.11.
70 * We now publish wheels for free-threaded Python 3.13, for PyPy 3.11 on
71 ``manylinux``, and for ARMv7l on ``manylinux``.
72
73 4.2.1
74 -----
75
76 * Bump Rust dependency versions - this should resolve crashes on Python 3.13
77 free-threaded builds.
78 * We no longer build ``manylinux`` wheels for PyPy 3.9.
79
80 4.2.0
81 -----
82
83 * Bump Rust dependency versions
84 * Removed the ``BCRYPT_ALLOW_RUST_163`` environment variable.
85
86 4.1.3
87 -----
88
89 * Bump Rust dependency versions
90
91 4.1.2
92 -----
93
94 * Publish both ``py37`` and ``py39`` wheels. This should resolve some errors
95 relating to initializing a module multiple times per process.
96
97 4.1.1
98 -----
99
100 * Fixed the type signature on the ``kdf`` method.
101 * Fixed packaging bug on Windows.
102 * Fixed incompatibility with passlib package detection assumptions.
103
104 4.1.0
105 -----
106
107 * Dropped support for Python 3.6.
108 * Bumped MSRV to 1.64. (Note: Rust 1.63 can be used by setting the ``BCRYPT_ALLOW_RUST_163`` environment variable)
109
110 4.0.1
111 -----
112
113 * We now build PyPy ``manylinux`` wheels.
114 * Fixed a bug where passing an invalid ``salt`` to ``checkpw`` could result in
115 a ``pyo3_runtime.PanicException``. It now correctly raises a ``ValueError``.
116
117 4.0.0
118 -----
119
120 * ``bcrypt`` is now implemented in Rust. Users building from source will need
121 to have a Rust compiler available. Nothing will change for users downloading
122 wheels.
123 * We no longer ship ``manylinux2010`` wheels. Users should upgrade to the latest
124 ``pip`` to ensure this doesn’t cause issues downloading wheels on their
125 platform. We now ship ``manylinux_2_28`` wheels for users on new enough platforms.
126 * ``NUL`` bytes are now allowed in inputs.
127
128
129 3.2.2
130 -----
131
132 * Fixed packaging of ``py.typed`` files in wheels so that ``mypy`` works.
133
134 3.2.1
135 -----
136
137 * Added support for compilation on z/OS
138 * The next release of ``bcrypt`` with be 4.0 and it will require Rust at
139 compile time, for users building from source. There will be no additional
140 requirement for users who are installing from wheels. Users on most
141 platforms will be able to obtain a wheel by making sure they have an up to
142 date ``pip``. The minimum supported Rust version will be 1.56.0.
143 * This will be the final release for which we ship ``manylinux2010`` wheels.
144 Going forward the minimum supported manylinux ABI for our wheels will be
145 ``manylinux2014``. The vast majority of users will continue to receive
146 ``manylinux`` wheels provided they have an up to date ``pip``.
147
148
149 3.2.0
150 -----
151
152 * Added typehints for library functions.
153 * Dropped support for Python versions less than 3.6 (2.7, 3.4, 3.5).
154 * Shipped ``abi3`` Windows wheels (requires pip >= 20).
155
156 3.1.7
157 -----
158
159 * Set a ``setuptools`` lower bound for PEP517 wheel building.
160 * We no longer distribute 32-bit ``manylinux1`` wheels. Continuing to produce
161 them was a maintenance burden.
162
163 3.1.6
164 -----
165
166 * Added support for compilation on Haiku.
167
168 3.1.5
169 -----
170
171 * Added support for compilation on AIX.
172 * Dropped Python 2.6 and 3.3 support.
173 * Switched to using ``abi3`` wheels for Python 3. If you are not getting a
174 wheel on a compatible platform please upgrade your ``pip`` version.
175
176 3.1.4
177 -----
178
179 * Fixed compilation with mingw and on illumos.
180
181 3.1.3
182 -----
183 * Fixed a compilation issue on Solaris.
184 * Added a warning when using too few rounds with ``kdf``.
185
186 3.1.2
187 -----
188 * Fixed a compile issue affecting big endian platforms.
189 * Fixed invalid escape sequence warnings on Python 3.6.
190 * Fixed building in non-UTF8 environments on Python 2.
191
192 3.1.1
193 -----
194 * Resolved a ``UserWarning`` when used with ``cffi`` 1.8.3.
195
196 3.1.0
197 -----
198 * Added support for ``checkpw``, a convenience method for verifying a password.
199 * Ensure that you get a ``$2y$`` hash when you input a ``$2y$`` salt.
200 * Fixed a regression where ``$2a`` hashes were vulnerable to a wraparound bug.
201 * Fixed compilation under Alpine Linux.
202
203 3.0.0
204 -----
205 * Switched the C backend to code obtained from the OpenBSD project rather than
206 openwall.
207 * Added support for ``bcrypt_pbkdf`` via the ``kdf`` function.
208
209 2.0.0
210 -----
211 * Added support for an adjustible prefix when calling ``gensalt``.
212 * Switched to CFFI 1.0+
213
214 Usage
215 -----
216
217 Password Hashing
218 ~~~~~~~~~~~~~~~~
219
220 Hashing and then later checking that a password matches the previous hashed
221 password is very simple:
222
223 .. code:: pycon
224
225 >>> import bcrypt
226 >>> password = b"super secret password"
227 >>> # Hash a password for the first time, with a randomly-generated salt
228 >>> hashed = bcrypt.hashpw(password, bcrypt.gensalt())
229 >>> # Check that an unhashed password matches one that has previously been
230 >>> # hashed
231 >>> if bcrypt.checkpw(password, hashed):
232 ... print("It Matches!")
233 ... else:
234 ... print("It Does not Match :(")
235
236 KDF
237 ~~~
238
239 As of 3.0.0 ``bcrypt`` now offers a ``kdf`` function which does ``bcrypt_pbkdf``.
240 This KDF is used in OpenSSH's newer encrypted private key format.
241
242 .. code:: pycon
243
244 >>> import bcrypt
245 >>> key = bcrypt.kdf(
246 ... password=b'password',
247 ... salt=b'salt',
248 ... desired_key_bytes=32,
249 ... rounds=100)
250
251
252 Adjustable Work Factor
253 ~~~~~~~~~~~~~~~~~~~~~~
254 One of bcrypt's features is an adjustable logarithmic work factor. To adjust
255 the work factor merely pass the desired number of rounds to
256 ``bcrypt.gensalt(rounds=12)`` which defaults to 12):
257
258 .. code:: pycon
259
260 >>> import bcrypt
261 >>> password = b"super secret password"
262 >>> # Hash a password for the first time, with a certain number of rounds
263 >>> hashed = bcrypt.hashpw(password, bcrypt.gensalt(14))
264 >>> # Check that a unhashed password matches one that has previously been
265 >>> # hashed
266 >>> if bcrypt.checkpw(password, hashed):
267 ... print("It Matches!")
268 ... else:
269 ... print("It Does not Match :(")
270
271
272 Adjustable Prefix
273 ~~~~~~~~~~~~~~~~~
274
275 Another one of bcrypt's features is an adjustable prefix to let you define what
276 libraries you'll remain compatible with. To adjust this, pass either ``2a`` or
277 ``2b`` (the default) to ``bcrypt.gensalt(prefix=b"2b")`` as a bytes object.
278
279 As of 3.0.0 the ``$2y$`` prefix is still supported in ``hashpw`` but deprecated.
280
281 Maximum Password Length
282 ~~~~~~~~~~~~~~~~~~~~~~~
283
284 The bcrypt algorithm only handles passwords up to 72 characters, any characters
285 beyond that are ignored. To work around this, a common approach is to hash a
286 password with a cryptographic hash (such as ``sha256``) and then base64
287 encode it to prevent NULL byte problems before hashing the result with
288 ``bcrypt``:
289
290 .. code:: pycon
291
292 >>> password = b"an incredibly long password" * 10
293 >>> hashed = bcrypt.hashpw(
294 ... base64.b64encode(hashlib.sha256(password).digest()),
295 ... bcrypt.gensalt()
296 ... )
297
298 Compatibility
299 -------------
300
301 This library should be compatible with py-bcrypt and it will run on Python
302 3.8+ (including free-threaded builds), and PyPy 3.
303
304 Security
305 --------
306
307 ``bcrypt`` follows the `same security policy as cryptography`_, if you
308 identify a vulnerability, we ask you to contact us privately.
309
310 .. _`same security policy as cryptography`: https://cryptography.io/en/latest/security.html
311 .. _`standard library`: https://docs.python.org/3/library/hashlib.html#hashlib.scrypt
312 .. _`argon2_cffi`: https://argon2-cffi.readthedocs.io
313 .. _`cryptography`: https://cryptography.io/en/latest/hazmat/primitives/key-derivation-functions/#cryptography.hazmat.primitives.kdf.scrypt.Scrypt