问题描述
我正在尝试将我的登录/注册模块从python 2.7移植到python3.5.该代码在2.7下可以正常工作,但是当我将其全部移至3.5时,在进行密码比较时我得到了无效的警告.我必须将其加载到数据库中的代码如下:
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())
环境是Python 3.55,Django 2和Bcrypt.我确实需要一些帮助,并且花了数小时寻找答案,但实际上并没有找到答案.谢谢大家.
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.
推荐答案
由于python 3字符串默认为utf8,因此您需要将密码编码为utf8.
You need to encode the password as utf8 since python 3 string is by default utf8.
request.POST['password'].encode('utf-8')
这是 flask-bcrypt github 中的问题.
这篇关于无效的Salt与Python 3,Django和Bcrypt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!