软件和服务器的现代密码散列
bcrypt的Python项目详细描述
安装
要安装bcrypt,只需:
$ pip install bcrypt
注意,bcrypt应该很容易在linux上构建,只要您有一个c编译器、python的头(如果您没有使用pypy)和系统上可用的libffi库的头。
对于debian和ubuntu,以下命令将确保安装所需的依赖项:
$ sudo apt-get install build-essential libffi-dev python-dev
对于Fedora和RHEL衍生物,以下命令将确保安装所需的依赖项:
$ sudo yum install gcc libffi-devel python-devel
备选方案
虽然bcrypt仍然是密码存储的一个不错的选择,这取决于您的特定用例,但您可能还需要考虑使用scrypt(通过standard library或cryptography)或argon2id通过argon2_cffi。
更改日志
3.1.7
- 设置PEP517车轮制造的setuptools下限。
- 我们不再分发32位manylinux1wheels。继续生产 它们是维护的负担。
3.1.6
- 增加了对俳句编译的支持。
3.1.5
- 增加了对aix上编译的支持。
- 放弃了对Python2.6和3.3的支持。
- 已切换到对python 3使用abi3wheels。如果你没有得到 请在兼容平台上升级pip版本。
3.1.4
- 修正了MingW和Illumos上的编译。
3.1.3
- 修复了solaris上的编译问题。
- 对kdf使用的轮次太少时添加了警告。
3.1.2
- 修复了影响big-endian平台的编译问题。
- 修复了Python3.6上的无效转义序列警告。
- 修复了python 2上非utf8环境中的构建。
3.1.1
- 与cffi1.8.3一起使用时解析了UserWarning。
3.1.0
- 添加了对checkpw的支持,这是一种验证密码的方便方法。
- 输入$2y$盐时,确保获得$2y$哈希。
- 修正了$2a散列易受包装错误攻击的回归。
- 修正了在Alpine Linux下的编译。
3.0.0
- 将c后端切换到从openbsd项目获得的代码,而不是 打开墙。
- 通过kdf函数添加了对bcrypt_pbkdf的支持。
2.0.0
- 在调用gensalt时添加了对可调整前缀的支持。
- 切换到CFFI 1.0+
使用量
密码哈希
散列,然后稍后检查密码是否与前一个散列匹配 密码非常简单:
>>> importbcrypt>>> 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>>> ifbcrypt.checkpw(password,hashed):... print("It Matches!")... else:... print("It Does not Match :(")
kdf
从3.0.0开始,bcrypt现在提供了一个kdf函数,它可以执行bcrypt_pbkdf。 这个kdf用在openssh新的加密私钥格式中。
>>> importbcrypt>>> key=bcrypt.kdf(... password=b'password',... salt=b'salt',... desired_key_bytes=32,... rounds=100)
可调工作系数
BCRYPT的一个特点是可调对数工作因子。调整 工作因子只需将所需的轮数传递给 bcrypt.gensalt(rounds=12)默认为12):
>>> importbcrypt>>> 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>>> ifbcrypt.checkpw(password,hashed):... print("It Matches!")... else:... print("It Does not Match :(")
可调前缀
bcrypt的另一个特性是一个可调整的前缀,允许您定义 您将保持与之兼容的库。若要对此进行调整,请传递2a或 2b(默认值)到bcrypt.gensalt(prefix=b"2b")作为字节对象。
从3.0.0开始,hashpw中仍然支持$2y$前缀,但已弃用。
最大密码长度
bcrypt算法只处理最多72个字符的密码,任何字符 除此之外,都被忽略了。为了解决这个问题mon方法是散列 密码和加密散列(例如sha256),然后是base64 在对结果进行哈希处理之前,对其进行编码以防止出现空字节问题 bcrypt:
>>> password=b"an incredibly long password"*10>>> hashed=bcrypt.hashpw(... base64.b64encode(hashlib.sha256(password).digest()),... bcrypt.gensalt()... )
兼容性
这个库应该与py bcrypt兼容,它将在python上运行 2.7,3.4+,和Pypy 2.6+。
C代码
这个库使用openbsd中的代码。
安全性
bcrypt跟在same security policy as cryptography后面,如果您 确定一个漏洞,我们要求您私下联系我们。