哈希函数没有为相同的inpu返回相同的输出

2024-06-06 15:42:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在努力实现一个网站,允许用户(虚构)买卖股票。这是哈瓦兹的CS50计算机科学课程的第9题。我的程序编译、运行并通过所有检查。然而,在过去的几天里,我一直在尝试实现密码更改选项。在

在密码更改页面上,我提示用户输入他们的旧密码。然后,用户必须输入他们想要的新密码。最后,用户必须再次确认他们的新密码。在

然而,当用户输入旧密码时,我的哈希函数输出的哈希值似乎与注册此密码时输出的哈希值不同。这导致我在数据库中哈希密码和用户输入之间的比较检查始终返回false。在

下面是实现密码更改的python代码。下面是用户可以更改密码的实际页面的html代码。在

@app.route("/change", methods=["GET", "POST"])
@login_required
# PERSONAL TOUCH: <Allows user to change their password>
def change():
"""Allows user to change their password"""

if request.method == "GET":
    return render_template("change.html")

else:

    # Returns an error if Password is left blank
    if not request.form.get("originalpassword"):
        return apology("User must submit their original Password.", 400)

    elif not request.form.get("newpassword"):
        return apology("User must submit a new Password.", 400)

    elif not request.form.get("newconfirmation"):
        return apology("User must confirm their new Password", 400)

    # Hashes the Password
    Password = request.form.get("originalpassword")
    print("GIVEN PASSWORD: ", Password)
    HashedPassword = generate_password_hash(Password, method='pbkdf2:sha256', salt_length=8)

    # Returns an error if the user typed in a non-valid original password
    OldHashedPassword = db.execute("SELECT hash FROM users WHERE id = :id", id=session["user_id"])
    Old_HPW = OldHashedPassword[0]["hash"]

    print("given hash: ", HashedPassword)
    print("actual hash: ", Old_HPW)

    print("=====\n",OldHashedPassword,"\n=====\n")

    if not Old_HPW == HashedPassword:
        return apology("Submitted password is not valid.")

    # Returns an error if Password and Confirmation are not identical
    if not request.form.get("newpassword") == request.form.get("confirmpassword"):
        return apology("New Password and Confirmation must be identical.", 400)

    # Hashes the new Password
    NewPassword = request.form.get("newpassword")
    NewHashedPassword = generate_password_hash(NewPassword, method='pbkdf2:sha256', salt_length=8)

    # Insert the new Password into the database
    insertPW = db.execute("INSERT INTO users (hash) VALUES(:hash)", hash=NewHashedPassword)

    return redirect("/")

以及HTML代码:

HTML Code

为了调试,我确实包含了一些print语句。我用用户名q和密码q注册了一个新帐户。然后,我试图把密码改成qq。在注册过程中,输入“q”导致以下哈希:

pbkdf2:sha256:50000美元sBxqbmza$E35DD4E61EB186AF014E5E1CE3B73450B1361BAABDD2F5559AD83EF0D5B45B

但是,当我在“原始密码”表单字段中输入“q”时,generate_password_uhash函数将返回哈希:

pbkdf2:sha256:50000$iAjKAPM1$F764F34F21864062EFA2F3E3F3EA0A89A8A3391A0223C1A62FA7CBAAB012A71

打印语句打印的值为:

给定密码:q

给定哈希:

pbkdf2:sha256:50000$iAjKAPM1$F764F34F21864062EFA2F3E3F3EA0A89A8A3391A0223C1A62FA7CBAAB012A71

实际哈希:

pbkdf2:sha256:50000美元sBxqbmza$E35DD4E61EB186AF014E5E1CE3B73450B1361BAABDD2F5559AD83EF0D5B45B

很抱歉我的帖子太长了。有人知道是什么原因导致哈希函数为(看起来)相似的输入输出不同的哈希值吗?在

谢谢!在

编辑:这个新代码似乎解决了这个问题:

NEW CODE


Tags: 用户form密码getreturnifrequestnot
1条回答
网友
1楼 · 发布于 2024-06-06 15:42:10

正如本文所指出的,answer有些库自己生成salt,您不提供。在这个库中,你只能提供盐的大小。从doc

werkzeug.security.generate_password_hash(password, method='pbkdf2:sha256', salt_length=8)

Hash a password with the given method and salt with a string of the given length. The format of the string returned includes the method that was used so that check_password_hash() can check the hash.

The format for the hashed string looks like this:

^{pr2}$

如果要存储salt,请从结果中解析它。但是这个库还有另一个检查/验证密码的功能

werkzeug.security.check_password_hash(pwhash, password)

check a password against a given salted and hashed password value.

所以

在注册登录,或更改密码使用

generate_password_hash

要验证密码,请使用

check_password_hash

相关问题 更多 >