Invalid Salt with Python 3, Django and Bcrypt

我与影子孤独终老i 提交于 2021-01-05 12:34:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!