1

bz2使用 Python使用模块加密密码时出现以下错误。在这里,我将加密值保存在 DB 中。

错误:

ProgrammingError at /signsave/
You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
Request Method: POST
Request URL:    http://127.0.0.1:8000/signsave/
Django Version: 1.11.2
Exception Type: ProgrammingError
Exception Value:    
You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

这是我的代码:

def signsave(request):
    """This function helps to save signup data"""

    if request.method == 'POST':
        name = request.POST.get('uname')
        password = request.POST.get('pass')
        con_pass = request.POST.get('conpass')
        new_pass = bz2.compress(password) 
        if password == con_pass:
            passw = User(
                uname=name,
                password=new_pass,
                raw_password=password,
            )
            passw.save()
            message = "Registered successfully"
            return render(request, 'bookingservice/login.html', {'msg': message})
        else:
            message = "The password did not match "
            return render(request, 'bookingservice/signup.html', {'msg': message})

在这里,当我尝试保存加密值时,这些错误即将到来。

4

1 回答 1

-1

bz2除了压缩,你不应该使用任何东西。请改用内置hashlib模块。

替换bz2.compress(passwordhashlib.sha256(str.encode(password)).digest()。您将获得密码字符串的 SHA256 哈希值,您可以将其与其他字符串的哈希值进行对比,以证明其有效性。

于 2017-07-10T12:34:22.653 回答