Skip to content

Instantly share code, notes, and snippets.

@BinaryKitten
Created March 22, 2016 21:17
Show Gist options
  • Save BinaryKitten/0ae4f8131a973ee13ffc to your computer and use it in GitHub Desktop.
Save BinaryKitten/0ae4f8131a973ee13ffc to your computer and use it in GitHub Desktop.
xkcd-password-wordpress.php
<?php
/*
* Generate xkcd style password from /usr/share/dict/words
*
* http://xkcd.com/936/
* apt-get install wamerican
*/
if (!function_exists('wp_generate_password')) :
$filesize = @filesize('/usr/share/dict/words');
if ($filesize !== false):
/**
* Generates a random password drawn from the defined set of characters.
*
* @since 2.5.0
*
* @param int $length Optional. The length of password to generate. Default 12.
* @param bool $special_chars Optional. Whether to include standard special characters.
* Default true.
* @param bool $extra_special_chars Optional. Whether to include other special characters.
* Used when generating secret keys and salts. Default false.
* @return string The random password.
*/
function wp_generate_password($length = 12, $special_chars = true, $extra_special_chars = false)
{
$lines = file('/usr/share/dict/words', FILE_IGNORE_NEW_LINES);
$length = count($lines);
$pw = array();
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[] = $lines[$key];
}
}
}
return implode('-', $pw);
}
endif;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment