Created
August 20, 2017 21:19
-
-
Save JackZielke/3804dc250e939407f5094368f7aa8c0b to your computer and use it in GitHub Desktop.
Generate a visual copy and paste friendly random password using OpenSSL and PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/php | |
| <?php | |
| $length = 10; | |
| if ($argc > 1) { | |
| if ($argv[1] == '-h' || $argv[1] == '--help') { | |
| echo basename($argv[0]) . ' [-h|--help] [length]' . PHP_EOL; | |
| echo 'length defaults to 10' . PHP_EOL; | |
| exit; | |
| } elseif ((int) $argv[1] > 0) { | |
| $length = $argv[1]; | |
| } | |
| } | |
| $hash = ''; | |
| while (strlen($hash) < $length) { | |
| // $hash .= base64_encode(mt_rand().mt_rand()); | |
| $hash .= base64_encode(openssl_random_pseudo_bytes($length)); | |
| $hash = preg_replace('/[^a-zA-Z0-9]/', '', $hash); // Base 62 | |
| $hash = str_replace(str_split('O0Il12Z5S'), '', $hash); // avoid pain with visual copy/paste | |
| $hash = preg_replace(array('/v+/','/V+/'), array('v', 'V'), $hash); | |
| } | |
| $hash = substr($hash, 0, $length); | |
| echo $hash . PHP_EOL; | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a script that I have been using since 2013 to generate passwords for web page registrations.