Skip to content

Instantly share code, notes, and snippets.

@JackZielke
Created August 20, 2017 21:19
Show Gist options
  • Select an option

  • Save JackZielke/3804dc250e939407f5094368f7aa8c0b to your computer and use it in GitHub Desktop.

Select an option

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
#!/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;
?>
@JackZielke
Copy link
Author

This is a script that I have been using since 2013 to generate passwords for web page registrations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment