- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在致力于实现一个允许用户(虚构地)买卖股票的网站。这是哈佛大学 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 代码:
为了调试,我确实包含了一些打印语句。我用用户名“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
很抱歉发了这么长的帖子。有谁知道是什么导致哈希函数为(看似)相似的输入输出不同的哈希值?
谢谢!
编辑:这个新代码似乎已经解决了问题:
最佳答案
正如本 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/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在编码时,我问了自己这个问题: 这样更快吗: if(false) return true; else return false; 比这个? if(false) return true; return
如何在逻辑条件下进行“返回”? 在这样的情况下这会很有用 checkConfig() || return false; var iNeedThis=doSomething() || return fa
这是我的正则表达式 demo 如问题所述: 如果第一个数字是 1 则返回 1 但如果是 145 则返回 145 但如果是 133 则返回 133 样本数据a: K'8134567 K'81345678
在代码高尔夫问答部分查看谜题和答案时,我遇到了 this solution返回 1 的最长和最晦涩的方法 引用答案, int foo(void) { return! 0; } int bar(
我想在下面返回 JSON。 { "name": "jackie" } postman 给我错误。说明 Unexpected 'n' 这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来
只要“is”返回 True,“==”不应该返回 True 吗? In [101]: np.NAN is np.nan is np.NaN Out[101]: True In [102]: np.NAN
我需要获取所有在 6 号或 7 号房间或根本不在任何房间的学生的详细信息。如果他们在其他房间,简单地说,我不希望有那个记录。 我的架构是: students(roll_no, name,class,.
我有一个表单,我将它发送到 php 以通过 ajax 插入到 mysql 数据库中。一切顺利,php 返回 "true" 值,但在 ajax 中它显示 false 消息。 在这里你可以查看php代码:
我在 Kotlin 中遇到了一个非常奇怪的无法解释的值比较问题,以下代码打印 假 data class Foo ( val a: Byte ) fun main() { val NUM
请注意,这并非特定于 Protractor。问题在于 Angular 2 的内置 Testability service Protractor 碰巧使用。 Protractor 调用 Testabil
在调试窗口中,以下表达式均返回 1。 Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & "
我在本地使用 jsonplaceholder ( http://jsonplaceholder.typicode.com/)。我正在通过 extjs rest 代理测试我的 GET 和 POST 调用
这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。 var client = new RestClient("http://sub.example.com/wp-json/wp/v2
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我想我对 C 命令行参数有点生疏。我查看了我的一些旧代码,但无论这个版本是什么,都会出现段错误。 运行方式是 ./foo -n num(其中 num 是用户在命令行中输入的数字) 但不知何故它不起作用
我已经编写了一个类来处理命名管道连接,如果我创建了一个实例,关闭它,然后尝试创建另一个实例,调用 CreateFile() 返回 INVALID_HANDLE_VALUE,并且 GetLastErro
即使 is_writable() 返回 true,我也无法写入文件。当然,该文件存在并且显然是可读的。这是代码: $file = "data"; echo file_get_contents($fil
下面代码中的变量 $response 为 NULL,尽管它应该是 SOAP 请求的值。 (潮汐列表)。当我调用 $client->__getLastResponse() 时,我从 SOAP 服务获得了
我一直在网上的不同论坛上搜索答案,但似乎没有与我的情况相符的... 我正在使用 Windows 7,VS2010。 我有一个使用定时器来调用任务栏刷新功能的应用程序。在该任务栏函数中包含对 LoadI
我是一名优秀的程序员,十分优秀!