openssl rand

The OpenSSL command that outputs a chosen number of random bytes from a cryptographically secure generator, used to make keys, IVs and passwords.

In more detail

You give it a number of bytes and optionally an output format: -hex prints each byte as two hexadecimal characters and -base64 prints Base64 text. The bytes come from OpenSSL's cryptographically secure random number generator, so they are suitable for keys, unlike the general-purpose random functions in most programming languages. Keep the output secret and out of shell history and version control, because anyone who sees it holds the key.

Examples

openssl rand -hex 32 > db.key   # 32 random bytes, printed as 64 hex characters
openssl rand -hex 16 > media.iv

Related terms

Taught in

References