gpt4 book ai didi

python - 哈希函数不会为相同的输入返回相同的输出

转载 作者:行者123 更新时间:2023-11-28 00:40:24 24 4
gpt4 key购买 nike

我目前正在致力于实现一个允许用户(虚构地)买卖股票的网站。这是哈佛大学 CS50 计算机科学类(class)中的第 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

为了调试,我确实包含了一些打印语句。我用用户名“q”和密码“q”注册了一个新帐户。然后,我尝试将密码更改为'qq'。在注册期间,输入“q”产生以下哈希:

pbkdf2:sha256:50000$sBxqbmza$e35dd4e61eb186af014e5e1ce3b73450b1361baabdd2f5f4559ad83ef0d5b45b

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

pbkdf2:sha256:50000$iAjKAPM1$f7644f34f21864062efa2f3e3f3ea0a89a8a3391a0223c1a62fa7cbaab012a71

打印语句打印的值是:

给定密码:q

给定哈希:

pbkdf2:sha256:50000$iAjKAPM1$f7644f34f21864062efa2f3e3f3ea0a89a8a3391a0223c1a62fa7cbaab012a71

实际哈希:

pbkdf2:sha256:50000$sBxqbmza$e35dd4e61eb186af014e5e1ce3b73450b1361baabdd2f5f4559ad83ef0d5b45b

很抱歉发了这么长的帖子。有谁知道是什么导致哈希函数为(看似)相似的输入输出不同的哈希值?

谢谢!

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

NEW CODE

最佳答案

正如本 answer 中所指出的有些图书馆自己生成盐,你不提供。在此库中,您只能提供盐大小。来自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:

method$salt$hash

如果你想存储盐,从结果中解析它。但是这个库还有另一个功能来检查/验证密码;

werkzeug.security.check_password_hash(pwhash, password)

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

所以;

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

generate_password_hash

要验证密码,请使用;

check_password_hash

关于python - 哈希函数不会为相同的输入返回相同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53803875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com