samedi 5 mai 2018

python generate fixed size (16 bytes) real random string

I want to generate a 16 bytes long random string in python3. urandom(n) seems to be the way to go to get real random characters:

os.urandom(n):

Return a string of n random bytes suitable for cryptographic use.

As I need a 16 bytes random string, I thought this would do the job for me:

from os import urandom
import sys

rnd_bytes = urandom(16)
print(sys.getsizeof(rnd_bytes))

But this prints 49 (size). Some answers suggest encoding it using base64, so I tried to encode it using b64encode(rnd_bytes) from base64 which didn't help (size 57). How can I create a 16 bytes long string from urandom?




Aucun commentaire:

Enregistrer un commentaire