openssl Verified current stable Not installed? Security

Openssl / Generate 20 Random Bytes Base64

Generate 20 Random Bytes Base64

Generates 20 random bytes encoded in base64 using OpenSSL's random number generator.

$
Terminal
openssl rand -base64 20

When To Use

When generating session tokens for user authentication

Pro Tip

The output will include padding characters '='; consider stripping them if not required.

Anatomy of Output

Understanding the result

MTRkMjRmZDI2NDAwMjkwMTY3NzQ3OTJlMDg5ODZkNGM= Generated Base64

Base64 encoded representation of 20 random bytes.

Power User Variants

Optimized versions

openssl rand -base64 40

Generates 40 random bytes encoded in base64.

openssl rand -base64 60

Generates 60 random bytes encoded in base64.

Troubleshooting

Common pitfalls

error setting key

Solution: Ensure OpenSSL has access to the required entropy sources.

invalid number of bytes

Solution: Specify the number of bytes in multiples compatible with the encoding.

out of memory

Solution: Check system load and available resources.

Command Breakdown

What each part is doing

openssl
Base Command
The executable that performs this operation. Here it runs Openssl before the shell applies any redirect operators.
-base64
Command Option
Tool-specific option used by this command invocation.

Alternative Approaches

Comparable commands in other tools

Alternative security tools for the same job.