Working with flask python. When i try to compare the hashed password encrypted with bcrypt.hashpw() say invalid salt.
Now i test it and if i put this if statement woks but only if in the password field in the html form i paste the password hashed (the thing is that i need to put the normal password) So the comparation i ok the form fiel in html and the connection to the db to take the user password works only fails when i need to put bcrypt.checkpw(password_in_the_login_form.html, password_hashed_in_the_db) That says Invalid Salt every time, here my code:
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '01b0cfa6c076da9264d7ea8b44967445'
app.config['MYSQL_DB'] = 'flaskdb'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['Email']
password = request.form['Password']
curl = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
curl.execute("SELECT * FROM users WHERE Email=%s",(email,))
user = curl.fetchone()
if len(user) > 0:
if user["Password"] == password:
session['Email'] = email
session['Nickname'] = user["Nickname"]
return render_template("profile.html")
else:
return "Error password and email not match"
else:
return "Error user not found"
else:
return render_template("login.html")
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for("home"))
@app.route("/signup", methods=["GET", "POST"])
def signup():
if request.method == 'GET':
return render_template("signup.html")
else:
nickname = request.form['Nickname']
firstName = request.form['First_Name']
lastName = request.form['Last_Name']
email = request.form['Email']
password = request.form['Password'].encode('utf-8')
hash_password = bcrypt.hashpw(password, bcrypt.gensalt(14))
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users (Nickname, First_Name, Last_Name,
Email, Password) VALUES (%s,%s,%s,%s,%s)",(nickname,firstName,lastName,email,hash_password,))
mysql.connection.commit()
return redirect(url_for('home'))
if __name__ == '__main__':
app.secret_key = 'bb8ef1d0a8de0bf09b4b2aaee861a7d5'
app.run(debug=True)
And the html forms
<form action="/signup" method="POST">
<input type="text" class="form-field" name="Nickname" placeholder="Nickname">
<input type="text" class="form-field" name="First_Name" placeholder="First Name">
<input type="text" class="form-field" name="Last_Name" placeholder="Last Name">
<input type="email" class="form-field" name="Email" placeholder="Email">
<input type="password" class="form-field" name="Password" placeholder="Password">
<button type="submit" class="signup-btn">SIGNUP</button>
</form>
<form action="/login" method="POST">
<input type="email" class="form" name="Email"><br>
<input type="password" class="form" name="Password"><br>
<button type="submit" class="login-btn">LOGIN</button>
</form>