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…
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.
You only have to decide the byte-length of your password or string, and OpenSSL does all the calculations.
For example an 8 byte pseudo-random string, hex encoded output:
$ openssl rand -hex 8
28dbc04b1a90fbf4
Code language: Bash (bash)
Or an 8 byte random string, base64 encoded output:
$ openssl rand -base64 8
7UON8PQIYHg=
Code language: Bash (bash)
OpenSSL on Windows
Generate random passwords in Windows using OpenSSL
If you have installed OpenSSL on Windows, you can use the same openssl command on Windows to generate a pseudo-random password or string:
c:\Users\Jan>C:\OpenSSL-Win64\bin\openssl.exe rand -hex 8
33247ca41c60ac53
Code language: PowerShell (powershell)
PHP OpenSSL – create a pseudo-random password with PHP and OpenSSL
In PHP you can use openssl_random_pseudo_bytes(), with bin2hex() for readability:
<?php
var_dump( bin2hex( 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)