Skip to content

Instantly share code, notes, and snippets.

@bendiy
Last active August 19, 2021 13:20
Show Gist options
  • Save bendiy/5688443 to your computer and use it in GitHub Desktop.
Save bendiy/5688443 to your computer and use it in GitHub Desktop.
PHP xkcd password generator for *nix systems
<?php
/*
* Generate xkcd style password from /usr/share/dict/words
*
* http://xkcd.com/936/
* apt-get install wamerican
*/
function xkcd_password_generator() {
$lines = file('/usr/share/dict/words', FILE_IGNORE_NEW_LINES);
$length = count($lines);
$pw = '';
for ($i = 1; $i <= 4; $i++) {
$plain = FALSE;
while (!$plain) {
// Get random word from $lines
$key = mt_rand(0, $length);
if ((preg_match("/^[a-z]+$/", $lines[$key]) == 1) && (strlen($lines[$key]) < 9)) {
// String only contains a to z characters
$plain = TRUE;
$pw = $pw . $lines[$key];
}
}
}
return $pw;
}
echo xkcd_password_generator() . "\n";
?>
@TimD2f
Copy link

TimD2f commented Aug 19, 2021

Cheers just what I needed

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