【问题标题】:Unique Salt per User using Flask-Security使用 Flask-Security 每个用户的唯一盐
【发布时间】:2014-11-14 12:44:37
【问题描述】:

在这里阅读了一些关于加盐密码的内容后,似乎最好为每个用户使用唯一的盐。我正在实施 Flask-Security atm,从文档看来,您只能设置一个全局盐:即 SECURITY_PASSWORD_SALT = 'thesalt'

问题:如何为每个密码制作唯一的盐?

谢谢!

编辑:从 Flask-Security 的文档中,我发现了这一点,这似乎再次表明该模块仅对所有开箱即用的密码使用单一盐。

flask_security.utils.get_hmac(password)
    Returns a Base64 encoded HMAC+SHA512 of the password signed with the salt 
    specified by SECURITY_PASSWORD_SALT.

【问题讨论】:

  • 全局盐不是盐。如果每个人都使用相同的盐,那么碰巧拥有相同密码的两个用户将拥有相同的哈希密码。这就是盐旨在防止的那种事情。
  • @MichaelBurr 好的,这就是我的想法……这就是我感到困惑的原因。如何使用此设置为每个密码制作唯一的盐?或者我是否必须覆盖 Flask-Security 内置的盐渍功能才能做到这一点?
  • 对不起 - 我对 Flask 一无所知。我只是在评论全球盐的想法。

标签: python encryption flask salt flask-security


【解决方案1】:

是的,如果使用 bcrypt(以及其他方案,例如 des_crypt、pbkdf2_sha256、pbkdf2_sha512、sha256_crypt、sha512_crypt),Flask-Security 确实会根据设计使用每个用户的盐。

“SECURITY_PASSWORD_SALT”的配置仅用于 HMAC 加密。如果您使用 bcrypt 作为散列算法,Flask-Security 使用 passlib 进行散列,并在散列期间生成随机盐。问题 268 中指出了这种混淆:https://github.com/mattupstate/flask-security/issues/268

可以在代码中验证,从encrypt走到passlib:

flask_security/utils.py(第 143-151、39 和 269 行)

def encrypt_password(password):
   ...
   return _pwd_context.encrypt(signed)

_pwd_context = LocalProxy(lambda: _security.pwd_context)

flask_security/core.py(269、244-251 和 18)

pwd_context=_get_pwd_context(app)

def _get_pwd_context(app):
    ...
    return CryptContext(schemes=schemes, default=pw_hash, deprecated=deprecated)

from passlib.context import CryptContext

最后来自:https://pythonhosted.org/passlib/password_hash_api.html#passlib.ifc.PasswordHash.encrypt

请注意,每次调用 encrypt() 都会生成一个新盐,

【讨论】:

    【解决方案2】:

    事实证明,如果您使用 bcrypt,它会处理加盐并将其与哈希一起存储。所以我会走那条路!

    感谢这个话题让我有了这个发现:

    Do I need to store the salt with bcrypt?

    【讨论】:

    • 感谢您在找到解决方案后回到您自己的问题。
    猜你喜欢
    • 2017-10-01
    • 2015-12-26
    • 2014-08-16
    • 2014-02-06
    • 2021-03-08
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多