简体   繁体   中英

I am getting a 500 server error in Flask when I try to bcrypt or scrypt a password

I'm new to Flask and was just learning my way around things by making a basic application, but I was having trouble with password storage. I am using postgresql and psycopg2 with this bcrypt package. Everything works fine if I don't try to protect the password at all or when I use SHA256, but when I attempt to use bcrypt, I get a 500 server error. Here's my code:

uname = request.form['uname']
passwd = bcrypt.hashpw(request.form['passwd'], bcrypt.gensalt())
conn = psycopg2.connect("dbname=flask user=postgres host=127.0.0.1 password=mypassword")
cur = conn.cursor()
cur.execute("INSERT INTO users (uname, passwd) VALUES (%s, %s)", (uname, passwd))
conn.commit()
cur.close()
conn.close()

Does anyone know why I am getting this problem? Could bcrypt just be taking to long? It didn't seem to take too long to load before I got the error back, but could that still be it? My main goal was to use bcrypt, but I swapped it out for scrypt just to check and that didn't work either.

I know this is an old question, but I hope people having the same problem will be helped quicker this way.

We had this problem, debug=true showed us what was wrong. The encoding wasn't correct for bcrypt. This should fix it.

passwd = bcrypt.hashpw(str(request.form['passwd']), bcrypt.gensalt())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM