Published on

How to Generate Random Secret OpenSSL


Whenever we work on a new project, we might need to generate a random secret for our projects like for example JWT secret which is used to sign the token or any other auth tokens.

Sure, we can generate it online using various apps but if you've security/privacy concerns and want to generate locally then OpenSSL might be a good choice for you.


If you don't have OpenSSL already in your machine, you can install it using various methods.

For macOS:

brew install openssl

And for others, you can refer to their docs

Generate random secrets

OpenSSL has rand command which we'll be using to generate secrets

The usage of it is like this:

# num -> length
openssl rand [-base64 | -hex] [-out file] num 

Generate random base64 secrets

openssl rand -base64 12
# dfzpJRkPLAguzfRQ

Generate random hex secrets

openssl rand -hex 12 
# 56705a986f27b8e9f8857ec1

Happy generating secrets!