openssl rand -base64 32 what is the equivalent in python

Question:

I would like to create a similar random base64 encoded string of 32 bytes, as

openssl rand -base64 32

in python

Can anyone help with, how this is done?

Asked By: Björk Dongson

||

Answers:

In python 3.6+:

from secrets import token_bytes
from base64 import b64encode

print(b64encode(token_bytes(32)).decode())

See https://docs.python.org/3/library/secrets.html for token security details.

Answered By: Stanislav Ivanov
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.