我已经尝试过使用烧瓶安全性,试图让它在各种场景中为我工作。我不需要加速哈希,所以我可能没有一个直接的答案,所以我只能为你指出正确的方向。
编辑 github 中的官方烧瓶安全性是this。我在下面使用的链接指向我的叉子,它有几个改动,所以如果你需要叉子,叉子官方的
1) Flask-Security 使用 passlib 进行散列
2) 如您分享的链接中所述,Flask-Security 使用 bcrypt,它故意变慢
3) Bcrypt 使用工作因子来确定您希望对数据进行哈希处理所需的时间。在 passlib 中,工作因子由称为 rounds 的 int 变量表示
4 为了散列数据,flask 安全使用flask-security/utils.py 中的以下函数
def hash_data(data):
return _hashing_context.hash(encode_string(data))
5 验证您的散列数据(例如令牌)它在同一文件中使用此函数
def verify_hash(hashed_data, compare_data):
return _hashing_context.verify(encode_string(compare_data), hashed_data)
6) hashing_context 是从 flask-security/core.py 中的 passlib 派生的,在文件的各个位置都像这样
from passlib.context import CryptContext
def _get_hashing_context(app):
schemes = cv('HASHING_SCHEMES', app=app)
deprecated = cv('DEPRECATED_HASHING_SCHEMES', app=app)
return CryptContext(
schemes=schemes,
deprecated=deprecated)
hashing_context=_get_hashing_context(app),
最后在 utils.py 中(之前的分享是这样的)
_hashing_context = LocalProxy(lambda: _security.hashing_context)
从 passlib 导入并用于上述 6 的 CryptContext 可以采用另一个参数,即在 4 到 31 之间舍入一个数字,默认为 12,根据docs 将舍入增加一 DOUBLES花费的时间
由于 CryptContext 在烧瓶安全性中没有传递给它的舍入值,我假设它使用默认值 12,因此您可以尝试各种数字来查看它是如何进行的。类似的东西
def _get_hashing_context(app):
schemes = cv('HASHING_SCHEMES', app=app)
deprecated = cv('DEPRECATED_HASHING_SCHEMES', app=app)
return CryptContext(
schemes=schemes,
deprecated=deprecated,
pbkdf2_sha256__default_rounds=your_rounds) #I added this line
因此,您需要分叉安全性,将您的轮次更改为适合您的数字并像这样安装您的回购
pip install git+https://github.com/your_repo/flask-security
信息量很大,希望对大家有用。
让我知道它的进展情况,因为我可能在不久的将来需要这个