django - Bcrypt Python3 Django:获取无效的盐错误
问题描述
所以只是尝试使用自定义验证制作一个基本的登录和注册样板。但是由于某种原因,在尝试登录时,我不断收到无效的 salt 错误。我可以补充一下,我的 bcrypt 设置方式在以前的项目中是有效的。但是当我升级到 Python3 时,出于某种原因它给我带来了这个错误。我开始在整个应用程序中放置打印语句,并将其缩小到我的登录表单验证器。以下是输出:-----
test display of logpassword: rrrrrr
user password: b'$2b$12$B3O9.UiaswKJvXkKAG2o9uqMHi5XrRBSyvDIPYwEa/o4AgyoGDww.'
what the encoded password we are seeing? b"b'$2b$12$B3O9.UiaswKJvXkKAG2o9uqMHi5XrRBSyvDIPYwEa/o4AgyoGDww.'"
what is the encoded password from logpassword? b'rrrrrr'
TEst 2 post data encode: b'rrrrrr'
将其与下面的代码进行比较,您会看到密码从注册中正确散列。但是,输入的帖子数据在编码时并未正确散列/编码,无法与数据库中的散列密码进行比较。非常感谢任何帮助,因为我已经坚持了很长一段时间了..
代码:-Views.py 只贴相关功能---
from django.shortcuts import render, redirect
from django.contrib import messages
from django.urls import reverse
from time import gmtime, strftime
import bcrypt
import re
from .models import *
from django.contrib import messages
def login(request):
if request.method == "POST":
#Check errors
errors = User.objects.LogForm_Validator(request.POST)
if len(errors):
# if the errors object contains anything, loop through each key-value pair and make a flash message
for key, value in errors.items():
messages.error(request, value)
return redirect('/')
else:
email = request.POST['logemail']
user = User.objects.get(email=email)
password = user.password
print("user: ", user)
print("Printed password: LOGFORM ", password)
print("email: ", email)
request.session['id'] = user.id
return redirect("/success")
def logout(request):
# del request.session['id']
request.session.clear()
return redirect("/")
def registration(request):
if request.method == "POST":
#Check errors
errors = User.objects.RegForm_Validator(request.POST)
if len(errors):
# if the errors object contains anything, loop through each key-value pair and make a flash message
for key, value in errors.items():
messages.error(request, value)
return redirect('/')
else:
fName = request.POST['fName']
lName = request.POST['lName']
email = request.POST['email']
birthdate = request.POST["birthday"]
password = request.POST['password']
hashedPass = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
print("PAssword: ", password)
print("hashed password: ", hashedPass)
print("Whats the salt: ", bcrypt.gensalt())
user = User.objects.create(fName=fName, lName=lName, email=email, birthdate=birthdate, password=hashedPass)
request.session['id'] = User.objects.get(email=email).id
users = User.objects.all().order_by("-created_at")
context = { "user": user, "users": users}
return render(request, "logreg_app/success.html", context)
Models.py ---- 下面是您将看到上述打印语句的地方。
from __future__ import unicode_literals
import bcrypt
import re
import datetime
from django.contrib import messages
class UserManager(models.Manager):
#Login Form:
def LogForm_Validator(self, postData):
errors = {}
email = postData['logemail']
user = User.objects.get(email=postData['logemail'])
print("Print user LOGFORM: ", user)
# Email Vlidations:
if not EMAILREGEX.match(postData['logemail']):
errors['email'] = "Your email must be valid characters only!"
if len(User.objects.filter(email=email)) == 0:
errors['user_exists'] = 'Account does not exist'
else:
print("test display of logpassword:", postData['logpassword'])
print("user password: ", user.password)
print("what the encoded password we are seeing?", user.password.encode());
# THIS RIGHT HERE IS THE ISSUE... THE ENCODED POSTDATE LOGPASSWORD, IS NOT BEING HASHED
print('what is the encoded password from logpassword? ', postData['logpassword'].encode())
print("TEst 2 post data encode: ", postData['logpassword'].encode())
if bcrypt.checkpw(postData['logpassword'].encode(), user.password.encode()):
print("password match")
else:
print("failed password")
errors['pw_match'] = 'Password Incorrect!'
return errors
解决方案
推荐阅读
- python - python tkinter中的缩进问题
- hibernate - 如何在 micronaut 中配置不同的休眠命名策略?
- linux - 软件包安装但未找到
- flask - Firefox 不允许 CORS 请求
- string - 可变参数模板函数 (printf) 的函数模板特化
- security - Raspberry Pi 家庭安全系统 - 请求指导
- python - 如何将 ä、ö、ü 等字符从 HTML 编码为 python?
- xaml - 如何在 Xamarin.Forms 中同时长按和短按列表项?
- python - free():Python 中的下一个大小无效
- android - react-native中视图之间的z顺序问题