使用Python加密密码时出错

时间:2017-07-10 12:21:18

标签: python bz2

使用Python使用bz2模块加密密码时出现以下错误。这里我将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})

这时当我试图保存加密值时,这些错误即将来临。

1 个答案:

答案 0 :(得分:-1)

除了压缩之外,你不应该使用bz2。请改用内置的hashlib模块。

bz2.compress(password替换为hashlib.sha256(str.encode(password)).digest()。您将获得密码字符串的SHA256哈希值,您可以检查其他字符串的哈希值,证明其有效性。