bcrypt
Create and verify OpenBSD-compatible `$2a$`, `$2b$` and `$2y$` bcrypt password records.
from oxcrypto.password.bcrypt import BCRYPT
algorithm = BCRYPT.BCRYPT_2B
encoded = algorithm.hash(b"example password")
assert algorithm.verify(b"example password", encoded)BCRYPT_2A, BCRYPT_2B and BCRYPT_2Y write the $2a$, $2b$ and $2y$
prefixes. The hash computation is the same for all three. verify returns
False for a record with another prefix and raises ValueError for a string
that is not a bcrypt record.
hash takes a keyword-only cost from 4 to 31 (default 12). Each step
doubles the work, and the record carries the cost:
encoded = algorithm.hash(b"example password", cost=14)
assert encoded.startswith("$2b$14$")bcrypt uses at most 72 bytes of the password. Longer passwords are truncated,
so two passwords with the same first 72 bytes verify against the same record.
Unlike C crypt(3), bytes after a NUL byte are part of the password.
Openwall's crypt_blowfish treats some $2a$ records with 8-bit passwords
differently (its countermeasure for the old $2x$ sign-extension bug). This
implementation computes $2a$ like $2b$, so such records do not verify.
bcrypt was published by Niels Provos and David Mazières (USENIX 1999) and
is the OpenBSD password scheme, a de facto standard without RFC or NIST
status. The implementation is the bcrypt crate 0.19.3 (no declared MSRV,
builds on Rust 1.88), a stable release with no published audit, using
the RustCrypto blowfish crate. Tests use vectors from Openwall
crypt_blowfish 1.3 and pyca/bcrypt.
The bcrypt crate is written to wipe its copy of the password, and oxcrypto
enables blowfish zeroization, with which the blowfish crate is written to
wipe the expanded key schedule when it is dropped. oxcrypto has not verified
this independently. Values on the stack, such as the 24-byte output block, are
not wiped.