You are here: Sysadmins of the North » GNU Linux » Generate pseudo-random passwords with OpenSSL

Generate pseudo-random passwords with OpenSSL

OpenSSL comes in handy when you need to generate random passwords, for example for system accounts and services. In this short post I’ll give you a quick example on how to generate random passwords with OpenSSL in Linux (Bash), Windows and PHP…

In this post you’ll learn how to create pseudo-random strings with OpenSSL. Random strings you can use as secure passwords.

Pseudo-random strings with OpenSSL

The OpenSSL rand command can be used to create random passwords for system accounts, services or online accounts. The rand command outputs num pseudo-random bytes after seeding the random number generator once. The -hex argument tells openssl to show the output as a hex string. You can also use -base64.

You only have to decide the byte-length of your password or string, whether you want hexadecimal or base64, and OpenSSL does all the calculations.

For example an 8 byte pseudo-random string, hex encoded output:

$ openssl rand -hex 8
28dbc04b1a90fbf4Code language: Bash (bash)

Or an 8 byte random string, base64 encoded output:

$ openssl rand -base64 8
7UON8PQIYHg=Code language: Bash (bash)

Using OpenSSL to generate random passwords in Windows

If you have installed OpenSSL on Windows, you can use the same openssl.exe command on Windows to generate a pseudo-random password or string:

PS C:\Users\Jan Reilink> &"C:\Program Files\OpenSSL-Win64\bin\openssl.exe" rand -hex 8
PS C:\Users\Jan Reilink> &"C:\Program Files\OpenSSL-Win64\bin\openssl.exe" rand -base64 8
tYHpFYAZ4C0=Code language: PowerShell (powershell)

These makes ideal passwords, easily generated with openssl.exe in Windows 11 or Windows 10 :) .

PHP OpenSSL – create a pseudo-random password with PHP and OpenSSL

In PHP you can use openssl_random_pseudo_bytes(), with bin2hex() for readability:

      openssl_random_pseudo_bytes( 8, $cstrong )
?>Code language: PHP (php)

Yes, hexadecimal strings are all lower-case… All you need now is a way to remember these generated strings and passwords… ;-)

Random passwords with PowerShell (#PSTip – Bonus)

Use PowerShell to create a random password:

 1..14 | ForEach {
  $my_passwd = $my_passwd + [char](New-Object System.Random( (get-date).Millisecond + $_ )).next(65,127)
Code language: PowerShell (powershell)

or add the following Get-RandomString function to your PowerShell $PROFILE.

Show Your Support

donate with Paypal

If you want to step in to help me cover the costs for running this website, that would be awesome. Just use this link to donate a cup of coffee ☕($10 USD or €10 EUR for example). And please share the love and help others make use of this website. Thank you very much! <3 ❤️

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top