跳转到主要内容

为您的软件和服务器提供现代密码散列

项目描述

bcrypt

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

为您的软件和服务器提供可接受的密码散列(但您实际上应该使用 argon2id 或 scrypt)

安装

要安装 bcrypt,只需

$ pip install bcrypt

注意,如果您的系统中已安装 C 编译器和 Rust 编译器,bcrypt 应该可以在 Linux 上非常容易地构建(最低支持的 Rust 版本是 1.56.0)。

对于 Debian 和 Ubuntu,以下命令将确保安装所需的依赖项

$ sudo apt-get install build-essential cargo

对于 Fedora 和 RHEL 衍生版,以下命令将确保安装所需的依赖项

$ sudo yum install gcc cargo

对于 Alpine,以下命令将确保安装所需的依赖项

$ apk add --update musl-dev gcc cargo

替代方案

虽然 bcrypt 仍然是一个可接受的密码存储选择,但根据您的特定用例,您可能还希望考虑使用 scrypt(通过 标准库cryptography)或通过 argon2_cffi 使用 argon2id。

变更日志

4.2.0

  • 提升 Rust 依赖版本

  • 移除了环境变量 BCRYPT_ALLOW_RUST_163

4.1.3

  • 提升 Rust 依赖版本

4.1.2

  • 发布 py37py39 轮文件。这应该可以解决与每个进程多次初始化模块相关的某些错误。

4.1.1

  • 修复了 kdf 方法的类型签名。

  • 修复了 Windows 上的打包错误。

  • 修复了与 passlib 包检测假设的不兼容性。

4.1.0

  • 停止支持 Python 3.6。

  • 将 MSRV 提升至 1.64。(注意:可以通过设置 BCRYPT_ALLOW_RUST_163 环境变量来使用 Rust 1.63)

4.0.1

  • 我们现在构建 PyPy manylinux 轮文件。

  • 修复了将无效的 salt 传递给 checkpw 可能导致 pyo3_runtime.PanicException 的错误。现在它正确地引发了一个 ValueError

4.0.0

  • bcrypt 现在使用 Rust 实现。从源代码构建的用户需要有一个 Rust 编译器。下载轮文件的用户不会有任何变化。

  • 我们不再提供 manylinux2010 轮文件。用户应升级到最新的 pip,以确保这不会在他们的平台上下载轮文件时引起问题。我们现在为足够新的平台上的用户提供 manylinux_2_28 轮文件。

  • 现在允许在输入中包含 NUL 字节。

3.2.2

  • 修复了轮文件中 py.typed 文件的打包,以便 mypy 可以工作。

3.2.1

  • 添加了对 z/OS 上编译的支持。

  • bcrypt 的下一个版本将是 4.0,它将在编译时需要 Rust,对于从源代码构建的用户。对于从轮文件安装的用户,没有额外的要求。大多数平台上的用户可以通过确保他们有一个最新的 pip 来获取轮文件。最低支持的 Rust 版本是 1.56.0。

  • 这将是最后提供 manylinux2010 轮文件的版本。从现在起,我们轮文件的最低支持的 manylinux ABI 将是 manylinux2014。绝大多数用户将继续接收 manylinux 轮文件,只要他们有一个最新的 pip

3.2.0

  • 为库函数添加了类型提示。

  • 停止支持低于 3.6 的 Python 版本(2.7,3.4,3.5)。

  • 发布了 abi3 Windows 轮文件(需要 pip >= 20)。

3.1.7

  • 为 PEP517 轮文件构建设置了 setuptools 下限。

  • 我们不再分发 32 位 manylinux1 轮文件。继续生产它们是一个维护负担。

3.1.6

  • 添加了对 Haiku 上编译的支持。

3.1.5

  • 添加了对 AIX 上编译的支持。

  • 停止支持 Python 2.6 和 3.3。

  • 将 Python 3 的轮文件切换到使用 abi3。如果您在兼容平台上没有获取到轮文件,请升级您的 pip 版本。

3.1.4

  • 修复了 mingw 和 illumos 上的编译问题。

3.1.3

  • 修复了 Solaris 上的编译问题。

  • kdf 中使用太少的轮数时添加了警告。

3.1.2

  • 修复了影响大端平台的编译问题。

  • 修复了 Python 3.6 上的无效转义序列警告。

  • 修复了 Python 2 上在非 UTF8 环境中构建的问题。

3.1.1

  • 解决了与 cffi 1.8.3 一起使用时出现的 UserWarning

3.1.0

  • 添加了对 checkpw 的支持,这是一个用于验证密码的便利方法。

  • 确保当您输入一个 $2y$ salt 时,您得到一个 $2y$ 散列。

  • 修复了 $2a 散列容易受到环绕错误的影响的回归问题。

  • 修复了 Alpine Linux 下的编译问题。

3.0.0

  • 将 C 后端切换到从 OpenBSD 项目获得的代码,而不是 openwall。

  • 通过 kdf 函数添加了对 bcrypt_pbkdf 的支持。

2.0.0

  • 当调用 gensalt 时增加了可调整的前缀支持。

  • 切换到 CFFI 1.0+

用法

密码哈希

哈希密码然后检查密码是否与之前的哈希密码匹配非常简单

>>> 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

从 3.0.0 版本开始,bcrypt 现在提供了一个 kdf 函数,它实现了 bcrypt_pbkdf。此 KDF 用于 OpenSSH 的新加密私钥格式。

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

可调整的工作因子

bcrypt 的一个特性是可调整的对数工作因子。要调整工作因子,只需将所需的轮数传递给 bcrypt.gensalt(rounds=12),默认为 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 :(")

可调整的前缀

bcrypt 的另一个特性是可调整的前缀,允许您定义与哪些库保持兼容。要调整此设置,请传递 2a2b(默认值)到 bcrypt.gensalt(prefix=b"2b"),作为字节对象。

从 3.0.0 版本开始,$2y$ 前缀在 hashpw 中仍然受支持,但已弃用。

最大密码长度

bcrypt 算法仅处理最多 72 个字符的密码,超出部分的字符将被忽略。为了解决这个问题,常用的方法是使用加密哈希(如 sha256)来哈希密码,然后使用 base64 编码以防止 NULL 字节问题,在将结果与 bcrypt 哈希之前。

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

兼容性

此库应与 py-bcrypt 兼容,并且可在 Python 3.6+ 和 PyPy 3 上运行。

安全性

bcrypt 遵循与 cryptography 相同的安全策略,如果您发现漏洞,请私下与我们联系。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

bcrypt-4.2.0.tar.gz (24.3 kB 查看哈希值)

上传时间 源代码

构建分发版

bcrypt-4.2.0-pp310-pypy310_pp73-manylinux_2_28_x86_64.whl (272.2 kB 查看哈希值)

上传时间 PyPy manylinux: glibc 2.28+ x86-64

bcrypt-4.2.0-pp310-pypy310_pp73-manylinux_2_28_aarch64.whl (275.9 kB 查看哈希值)

上传时间: PyPy manylinux: glibc 2.28+ ARM64

bcrypt-4.2.0-pp39-pypy39_pp73-manylinux_2_28_x86_64.whl (274.1 kB 查看哈希值)

上传时间: PyPy manylinux: glibc 2.28+ x86-64

bcrypt-4.2.0-pp39-pypy39_pp73-manylinux_2_28_aarch64.whl (278.1 kB 查看哈希值)

上传时间: PyPy manylinux: glibc 2.28+ ARM64

bcrypt-4.2.0-cp39-abi3-win_amd64.whl (151.7 kB 查看哈希值)

上传时间: CPython 3.9+ Windows x86-64

bcrypt-4.2.0-cp39-abi3-win32.whl (155.9 kB 查看哈希值)

上传时间: CPython 3.9+ Windows x86

bcrypt-4.2.0-cp39-abi3-musllinux_1_2_x86_64.whl (335.7 kB 查看哈希值)

上传时间: CPython 3.9+ musllinux: musl 1.2+ x86-64

bcrypt-4.2.0-cp39-abi3-musllinux_1_2_aarch64.whl (325.4 kB 查看哈希值)

上传时间: CPython 3.9+ musllinux: musl 1.2+ ARM64

bcrypt-4.2.0-cp39-abi3-musllinux_1_1_x86_64.whl (306.4 kB 查看哈希值)

上传时间: CPython 3.9+ musllinux: musl 1.1+ x86-64

bcrypt-4.2.0-cp39-abi3-musllinux_1_1_aarch64.whl (311.3 kB 查看哈希值)

上传时间: CPython 3.9+ musllinux: musl 1.1+ ARM64

bcrypt-4.2.0-cp39-abi3-manylinux_2_28_x86_64.whl (273.8 kB 查看哈希值)

上传时间: CPython 3.9+ manylinux: glibc 2.28+ x86-64

bcrypt-4.2.0-cp39-abi3-manylinux_2_28_aarch64.whl (277.9 kB 查看哈希值)

上传时间: CPython 3.9+ manylinux: glibc 2.28+ ARM64

bcrypt-4.2.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (273.5 kB 查看哈希值)

上传时间: CPython 3.9+ manylinux: glibc 2.17+ x86-64

bcrypt-4.2.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (277.6 kB 查看哈希值)

上传时间: CPython 3.9+ manylinux: glibc 2.17+ ARM64

bcrypt-4.2.0-cp39-abi3-macosx_10_12_universal2.whl (472.4 kB 查看哈希值)

上传时间: CPython 3.9+ macOS 10.12+ universal2 (ARM64, x86-64)

bcrypt-4.2.0-cp37-abi3-win_amd64.whl (151.3 kB 查看哈希值)

上传时间: CPython 3.7+ Windows x86-64

bcrypt-4.2.0-cp37-abi3-win32.whl (156.5 kB 查看哈希值)

上传时间: CPython 3.7+ Windows x86

bcrypt-4.2.0-cp37-abi3-musllinux_1_2_x86_64.whl (335.2 kB 查看哈希值)

上传时间: CPython 3.7+ musllinux: musl 1.2+ x86-64

bcrypt-4.2.0-cp37-abi3-musllinux_1_2_aarch64.whl (325.2 kB 查看哈希值)

上传时间: CPython 3.7+ musllinux: musl 1.2+ ARM64

bcrypt-4.2.0-cp37-abi3-musllinux_1_1_x86_64.whl (305.9 kB 查看哈希值)

上传时间: CPython 3.7+ musllinux: musl 1.1+ x86-64

bcrypt-4.2.0-cp37-abi3-musllinux_1_1_aarch64.whl (311.1 kB 查看哈希值)

上传时间: CPython 3.7+ musllinux: musl 1.1+ ARM64

bcrypt-4.2.0-cp37-abi3-manylinux_2_28_x86_64.whl (273.8 kB 查看哈希值)

上传时间: CPython 3.7+ manylinux: glibc 2.28+ x86-64

bcrypt-4.2.0-cp37-abi3-manylinux_2_28_aarch64.whl (277.8 kB 查看哈希值)

上传时间: CPython 3.7+ manylinux: glibc 2.28+ ARM64

bcrypt-4.2.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (273.5 kB 查看哈希值)

上传时间: CPython 3.7+ manylinux: glibc 2.17+ x86-64

bcrypt-4.2.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (277.4 kB 查看哈希值)

上传时间: CPython 3.7+ manylinux: glibc 2.17+ ARM64

bcrypt-4.2.0-cp37-abi3-macosx_10_12_universal2.whl (471.6 kB 查看哈希值)

上传时间: CPython 3.7+ macOS 10.12+ universal2 (ARM64, x86-64)

支持者