Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask sha256\u crypt.verify返回“不是有效的sha256\u crypt哈希”`_Python_Mysql_Flask_Sha256_Password Encryption - Fatal编程技术网

Python Flask sha256\u crypt.verify返回“不是有效的sha256\u crypt哈希”`

Python Flask sha256\u crypt.verify返回“不是有效的sha256\u crypt哈希”`,python,mysql,flask,sha256,password-encryption,Python,Mysql,Flask,Sha256,Password Encryption,我正在使用Flask作为web登录页。我试图利用sha256密码加密,但我不知道为什么行sha256\u crypt.verify(password,pass\u data)会抛出标题中的错误 我有什么遗漏吗?如果我能提供更多的细节,请询问,也许提供一些关于如何调试的说明。多谢各位 @app.route("/login", methods=["GET","POST"]) def login(): if request.method == "POST": username= st

我正在使用Flask作为web登录页。我试图利用sha256密码加密,但我不知道为什么行
sha256\u crypt.verify(password,pass\u data)
会抛出标题中的错误

我有什么遗漏吗?如果我能提供更多的细节,请询问,也许提供一些关于如何调试的说明。多谢各位

@app.route("/login", methods=["GET","POST"])
def login():
   if request.method == "POST":
      username= str(request.form['username'])
      password = request.form.get('password')
      cursor = mydb.cursor(MySQLdb.cursors.DictCursor)
      cursor.execute("SELECT * FROM user WHERE username ='"+ username +"'")
      userdata = cursor.fetchone()
      usernamedata = userdata['username']
      passworddata = userdata['password']
      if usernamedata is None:
        flash("Incorrect username","danger")
        return render_template("login.html")
     else:
        for pass_data in passworddata:
            if sha256_crypt.verify(password,pass_data):
                flash("You are now login","success")
                return redirect(url_for('profile'))
            else:
                flash("Incorrect password!")
                return render_template("login.html")
   return render_template("login.html")

假设passworddata是一个字符串,在passworddata中对pass_数据执行
操作只会在字符串的字母上循环。我认为改变这一点:

for pass_data in passworddata:
    if sha256_crypt.verify(password,pass_data):
        flash("You are now login","success")
        return redirect(url_for('profile'))
    else:
        flash("Incorrect password!")
        return render_template("login.html")
为此:

if sha256_crypt.verify(password, passworddata):
    flash("You are now login","success")
    return redirect(url_for('profile'))
else:
    flash("Incorrect password!")
    return render_template("login.html")
你应该把它修好

另外,不要像这样执行SQL

cursor.execute("SELECT * FROM user WHERE username ='"+ username +"'")

它很容易出错。

我认为每个人都可以很方便地看到实际的错误消息,这样您就不必猜测哪一行抛出异常。欢迎使用堆栈溢出!我编辑了您问题的标题,以包含您正在调用的函数的名称,这样更多了解该主题的人将看到它。有关格式设置的详细信息,请参阅编辑帮助。请在遇到的特定错误消息中进行编辑,以防识别特定问题。祝你好运谢谢你的评论。我设置了你的代码,但还是有错误。现在,错误显示为-ValueError:expected sha256_crypt hash,get sha256_crypt config string instead您如何在数据库中存储密码?下面是注册部分:if request.method==“POST”:username=str(request.form[“username”])email=str request.form[“email”])password=str(request.form[“password”])pwd_hash=sha256_crypt.encrypt(password)conpassword=str(request.form[“conpassword”]),如果password==conpassword:cursor=mydb.cursor()cursor.execute(“插入用户(,用户名,电子邮件,密码)值(%s,%s),(用户名,电子邮件,pwd_hash))mydb.commit()