-
-
Save iPublicis/583868a138a63be3bd88d40ad5a4d845 to your computer and use it in GitHub Desktop.
xkcd-password-wordpress.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
<?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