0

I know something similar has been asked before, but no answer has yet worked.

I make a byte string: salt = os.urandom(16) which gives something like: b'w\x05\xce^f\xdcbM\xe9\xb8c\x8b\x98\xd2\n\x11'

What I need is to give this to the user, so they can copy and paste, place it in a text document or anywhere, and then paste it back in the terminal later.

In short. I need to convert it to a string. And then back to its encoding.

I have tried salt.decode(encoding="utf-8") and many variations which all give me some form of UnicodeDecodeError: 'utf-8' codec can't..., The only one that seemed to work was "".join(map(chr, salt)), but I can't figure out how to reverse this.

Thanks in advance. P.s. I am working in Python 3

0

1 Answer 1

1

The python module binsascii is most likely what you need. Example:

>>> import os
>>> salt = os.urandom(16)
>>> import binascii
>>> binascii.b2a_hex(salt)
'9df7cc8d135fb9f115e166e140153217'
>>> binascii.a2b_hex(binascii.b2a_hex(salt))
'\x9d\xf7\xcc\x8d\x13_\xb9\xf1\x15\xe1f\xe1@\x152\x17'

A number of other encodings are also available.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.