Skip to content
Advertisement

How to just change type of string to bytes? python

I am using Fernet and want to setup a basic authentication system,

So on signup, I generate a key for fernet using key = Fernet.generate_key() Then I store that key in my cookies, so that it can be called during log in, but doing so converts the type of bytes to string.

Suppose my key was – b'BNdXABgpo_Y5PH3VNpSfAJo8Y7A-vdTTIN5WJxYRgpk='

and when I call from cookies its returned as – "b'BNdXABgpo_Y5PH3VNpSfAJo8Y7A-vdTTIN5WJxYRgpk='"

Is there a way to convert it back as before, like just converting the type

Any help is appreciated

Advertisement

Answer

I would convert the key to a string and then save it in the cookies like so:

key = key.decode("UTF-8")

When you need it you can later convert it back to bytes like so:

key = key.encode("UTF-8")
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement