Random Password Generator (based on word list)





15
Date Submitted Fri. Jan. 19th, 2007 2:37 PM
Revision 1 of 1
Helper inxilpro
Tags "word list" | Generator | Password | PHP | Random
Comments 0 comments
This is a random password generator that produces understandable passwords based on word lists. I've only included a 3 entry world list because you should chose a list based on your password requirements and your users. If you need to generate passwords that are 14 characters in length, you will want a different list than if you're generating 8 character passwords. And depending on your users, you may want to use certain lists. The list I use is about 4000 words that are 5-7 characters long, all well-known words that have had potentially objectionable content removed. For security reasons I don't want to include this list.

A note on security: though this generates relatively strong passwords for the average user, they are particularly susceptible to brute-force attacks. This is even more an issue if somehow your word list gets compromised. I would not recommend using this function for anything where a highly secure password is needed.

A note on choosing your list: You'll also see that I've built the system to avoid generating passwords with zeros and ones in them. This is because zero and upper-case "o" can be confused as can one, lower-case "L" and upper-case "i." When choosing my word list I was also sure to strip out all words that start with the letter "o" or "i" (to prevent the optional ucfirst() from creating 0/O and I/1 confusion) and words that contain the letter "L" (to prevent l/1 confusion). I find that this greatly helps with preventing confusion, but again weakens the security of the passwords some. It's your choice.

<?php
function generatePassword($maxLen = 8)
{
        $words = array('about', 'above', 'absence',); // Use your own list
       
        while (strlen($word) > $maxLen || !$word) $word = $words[array_rand($words, 1)];
        if (rand(0,1)) $word = ucfirst($word)
        for ($i = 0; $i < $maxLen - strlen($word); $i++) rand(0,1) ? $opener .= rand(2,9) : $closer .= rand(2,9);
       
        return "{$opener}{$word}{$closer}";
}
?>
 

for ($i = 0; $i < 20; $i++)
{
        echo generatePassword() . " ";
}
 
Produces: 6Above43 79Above7 absence4 2Above85 58about5 59Above3 4absence Absence3 6about44 3Absence Above587 Above994 8Above56 Above259 699above 4Above88 absence3 absence3 About322 267About

Chris M

Comments

There are currently no comments for this snippet.

Voting