【问题标题】:compare stored hash password with plaintext input user password将存储的哈希密码与明文输入的用户密码进行比较
【发布时间】:2021-10-23 13:29:59
【问题描述】:

我使用 bcrypt 库存储了一个哈希密码,因此它总是在我的数据库中存储不同的哈希字符串。如果字符串值不一样,如何将它们与商店密码进行比较?

@login.route('/log',methods=['POST'])
def login():
    error = None
    # get data from JSON
    body = request.get_json()

    # if data contains something
    if body != error:
        # Verification of POST method
        if request.method == 'POST':
            # bucle for empty values findings
            validation = all(x != "" for x in body.values())
            if validation:
                username_mod = body['username']
                password_mod = body['password_hash']
                forced = b"valentina"
                hashed = hashpw(password_mod.encode('utf-8'), gensalt())
                userMatch = User.query.filter_by(username=username_mod).first()
                store_password = userMatch.password_hash
                
                if checkpw(forced, hashed):
                    print("it matches")
                else:
                    print("they dont")
                
                
                if userMatch:
                    if checkpw(hashed, store_password):
                        pswd_match = True
                    else:
                        pswd_match = False

                if userMatch and pswd_match:
                    return msg_handler("user allowed", 200)
                else:
                    return msg_handler("user denied", 400)
            else:
                return msg_handler("missing value in 1 or more parameters", 400)
        else:
            return msg_handler("Must be POST method", 400)
    else:
        return msg_handler("no data", 400)
我附上了我的调试

【问题讨论】:

  • 加密输入密码并与db中的密码比较,尝试encrypt_pwd('pwd-goes-here') == 'encrypted-pwd-string-goes=here
  • 它们是不同的,因为 bcrypt 库总是更改哈希值。我的密码已经存储,然后我得到一个新字符串,我知道它与存储的值相同。

标签: python flask hash bcrypt


【解决方案1】:

在我看来,每次对密码进行哈希处理时,您都会生成一个新的盐。 salt 是一个随机生成的值,在哈希之前附加到密码。

如果您和我都选择abc123 作为我们的密码,那么我们密码的哈希值也将相同。如果有人发现我的密码是abc123,并且他们看到您的哈希值相同,那么他们也会知道您的密码。现在说我的密码是abc123,但是当它被散列时,它会在开头粘贴一些随机字节,比如7c6。然后它存储我的盐7c67c6abc123 的哈希值。您仍然使用abc123 作为密码,但它会随机生成9er 作为您的盐。然后它将9er 存储为你的盐,以及9erabc123 的哈希值。现在我们的哈希看起来不同了,即使我们的密码是一样的。

请注意,盐是未加密存储的。这样你就可以输入你的密码,它可以把盐贴在它的前面,然后散列盐 + 密码的组合。该哈希是需要与存储的哈希进行比较的。如果你每次都生成一个新的盐,那么每次的哈希值都会不同。

所以,这部分:

hashed = hashpw(password_mod.encode('utf-8'), gensalt())

不应生成新盐。它需要重新使用之前使用(和存储)的盐。

【讨论】:

  • 感谢您的帮助,您给出了定义和行为。我改变了我的代码,我确实使用了 werkzeug.security
  • 很高兴它有帮助!如果您认为答案正确回答了问题,请将答案标记为已接受。
【解决方案2】:

我使用 werkzeug.security 解决了我的问题 现在我的代码是这样的:

from werkzeug.security import generate_password_hash, check_password_hash

username_mod = body['username']
            password_mod = body['password_hash']
            userMatch = User.query.filter_by(username=username_mod).first()
            store_password = userMatch.password_hash

            if userMatch:
                if check_password_hash(store_password, password_mod ):
                    pswd_match = True
                else:
                    pswd_match = False

            if userMatch and pswd_match:
                return msg_handler("user allowed", 200)
            else:
                return msg_handler("user denied", 400)

【讨论】:

    猜你喜欢
    • 2017-08-05
    • 2016-03-06
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2019-07-02
    • 2014-08-10
    相关资源
    最近更新 更多