Bcrypt Python3 Django:获取无效的Salt错误

时间:2018-09-11 23:50:05

标签: django bcrypt salt

因此,这只是尝试通过自定义验证来制作基本的登录和注册样板。但是由于某种原因,当我尝试登录时,我不断收到无效的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

0 个答案:

没有答案