【问题标题】:hashnig passwords with argon2_cffi带有 argon2_cffi 的哈希密码
【发布时间】:2019-03-13 21:16:36
【问题描述】:

我正在尝试了解如何使用 argon2_cffi 在我的数据库中存储散列密码。

具体来说,我正在使用此代码将散列密码写入我的 PostgreSQL 表中。

from argon2 import PasswordHasher

ph = PasswordHasher()

new_user = User(
    name=POST.get('name', 'default_value'),
    fullname=POST.get('fullname', 'default_value'),
    nickname=POST.get('nickname', 'default_value'),
    hashed_password=ph.hash(POST.get('password', 'default_value')))
session.add(new_user)

但是,每次用户在我的表单中插入密码时,都会产生不同的密码,尽管插入的文本是相同的。

当然,我知道这是他正确的行为,但是如果我无法生成相同的哈希值,我应该怎么做才能验证给定的注册用户是否输入了正确的密码?

【问题讨论】:

    标签: python password-hash argon2-ffi


    【解决方案1】:

    抱歉,在the docs发现自己...

    import argon2
    
    ph = argon2.PasswordHasher()
    def login(db, user, password):
        hash = db.get_password_hash_for_user(user)
    
        # Verify password, raises exception if wrong.
        ph.verify(hash, password)
    
        # Now that we have the cleartext password,
        # check the hash's parameters and if outdated,
        # rehash the user's password in the database.
        if ph.check_needs_rehash(hash):
            db.set_password_hash_for_user(user, ph.hash(password))
    

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 2016-05-12
      • 2015-03-02
      • 1970-01-01
      • 2023-03-31
      • 2021-03-15
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      相关资源
      最近更新 更多