问题
I am trying to port my login/registration module from python 2.7 to python3.5. The code is working fine under 2.7 but when I moved it all to 3.5 I am now getting an invalid salt when I do the password comparison. The code I have to load it into the database is as follows:
password = bcrypt.hashpw(request.POST['password'].encode(),bcrypt.gensalt())
new_user = User.objects.create(first_name = first_name,
last_name=last_name, email = email, birthday = birthday)
Password.objects.create(pwd = password, user = User.objects.get
(id = new_user.id))enter code here
There are no errors when the information gets inserted. The code for the password check is then as follows:
user = User.objects.filter(email = request.POST['email'])[0]
password=bcrypt.checkpw(request.POST['password'].encode(),user.password.pwd.encode())
The environment is Python 3.55, Django 2 and Bcrypt. I really need some help on this and I have spent hours looking for an answer and not really able to find one. Thank you one and all.
回答1:
You need to encode the password as utf8 since python 3 string is by default utf8.
request.POST['password'].encode('utf-8')
This is the issue from the flask-bcrypt github.
来源:https://stackoverflow.com/questions/49891222/invalid-salt-with-python-3-django-and-bcrypt