使用 argon2_cffi 散列密码

hashnig passwords with argon2_cffi

我想了解我应该如何使用 argon2_cffi 在我的数据库中存储散列密码。

具体来说,我正在使用这段代码将散列密码写入我的 PostgreSQL table。

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)

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

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

抱歉,发现自己在 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))