简体   繁体   中英

Getting error while encrypting password using Python

I am getting the following error while encrypting the password using bz2 module using Python. Here I am saving that encrypted value inside DB.

Error:

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.

Here is my code:

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})

Here when I am trying to save the encrypted value those errors are coming.

You shouldn't use bz2 for anything but compression. Use built-in hashlib module instead.

Replace bz2.compress(password with hashlib.sha256(str.encode(password)).digest() . You'll get a SHA256 hash of your password string, which you can check against other string's hashes, proving their validity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM