ActiveState Code

Recipe 101526: Password Generation


Quite often you need to generate passwords for user authentication systems.

PHP
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

function make_password($length){
    $vowels = 'aeiouy';
    $consonants = 'bdghjlmnpqrstvwxz';
    $password = '';
    
    $alt = time() % 2;
    srand(time());

    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % 17)];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % 6)];
            $alt = 1;
        }
    }
    return $password;
}

echo make_password(8);
?>

Discussion

Years ago I found this function to create pseudo-english passwords when I needed something simple for a website I was working on. I've since simplified it, but it is essentialy the same. This works by inserting a vowel in between every consonant.

Comments

  1. 1. At 7:30 a.m. on 19 jan 2002, Christoph Schaper said:

    weak passwords. to my opinion passwords containing only lower case vowels and consonants are weak passwords. generated passwords should be a mixture between lower/uppercase alphabetic chars and numbers.

    maybe this function should be changed. but this is just my personal opinion

  2. 2. At 1:12 p.m. on 30 sep 2002, Jonathan Meyer said:

    Strong Passwords. This function could generate strong passwords. All one needs to do is modify the $vowels and $consonants variables to include both upper and lower case letters. One could even put numbers in either of these variables.

    I like this function simple and concise.

  3. 3. At 4:49 a.m. on 23 oct 2002, Michael Peters said:

    Stronger Password Generation. $vowels = 'aeiouyAEIOUY' $consonants = 'bdghjlmnpqrstvwxzBDGHJLMNPQRSTVWXZ' $specials = '1234567890@#$%^$vowels = 'aeiouyAEIOUY' $consonants = 'bdghjlmnpqrstvwxzBDGHJLMNPQRSTVWXZ' $specials = '1234567890@#$%^

  4. 4. At 4:42 a.m. on 29 nov 2002, Alan Prescott said:

    How about.

    /**
      password = make_password
    
      Added the strength option to allow various levels of
      strength of password.
      =0 - default, lower case only
      and 1 - Add in uppercase consonants
      and 2 - Add in upper case vowels
      and 4 - Add in numbers
      and 8 - Add in special characters
      So 15 would give everything
    **/
    function make_password($length=8,$strength=0) {
      $vowels = 'aeiouy';
      $consonants = 'bdghjlmnpqrstvwxz';
      if ($strength <pre>
    /**
      password = make_password
    
      Added the strength option to allow various levels of
      strength of password.
      =0 - default, lower case only
      and 1 - Add in uppercase consonants
      and 2 - Add in upper case vowels
      and 4 - Add in numbers
      and 8 - Add in special characters
      So 15 would give everything
    **/
    function make_password($length=8,$strength=0) {
      $vowels = 'aeiouy';
      $consonants = 'bdghjlmnpqrstvwxz';
      if ($strength
    

    </pre>

  5. 5. At 4:45 a.m. on 29 nov 2002, Alan Prescott said:

    Sorry about that. The posting went haywire - I think ActiveState have a problem with submitting comments. - Alan

Sign in to comment