How to Generate Random Secret using OpenSSL

Overview

I was building a new project recently, and needed to create a new random secret.

I took this as an opportunity to learn about security and brush up my skills like password hashing, salts, cryptographically secure keys, randomness etc.

After my rabbit hole journey, I got back to the task at hand.

TLDR: Have a Mac? openssl rand -hex 32.

Prerequisites

OpenSSL

For MacOS:

brew install openssl

On Windows:

Use this if you have Git installed:

C:\Program Files\Git\usr\bin\openssl.exe

Else, build it yourself: https://github.com/openssl/openssl/blob/master/NOTES-WINDOWS.md

Ubuntu

sudo apt install openssl

Generate random secrets

Base64

openssl rand -base64 32

Hex

openssl rand -hex 32

Try it out!

If you like rabbit holes, look up OS entropy and random generators.

References and Further Reading

  1. OpenSSL RAND_bytes man page: https://docs.openssl.org/1.0.2/man3/RAND_bytes/
  2. OpenSSL Library https://openssl-library.org/
  3. Brew OpenSSL https://formulae.brew.sh/formula/openssl@3
  4. Bcrypt https://en.wikipedia.org/wiki/Bcrypt