ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Password generation
Submitter: Alan Prescott (other recipes)
Last Updated: 2002/11/29
Version no: 1.0
Category: Security Solutions

 

4 stars 1 vote(s)


Approved

Description:

Improved version of make_password submitted by Shane Caraveo. This one adds options to use upper case characters, numerics and special characters.

Source: Text Source

function make_password($length,$strength=0) {
  $vowels = 'aeiouy';
  $consonants = 'bdghjlmnpqrstvwxz';
  if ($strength & 1) {
    $consonants .= 'BDGHJLMNPQRSTVWXZ';
  }
  if ($strength & 2) {
    $vowels .= "AEIOUY";
  }
  if ($strength & 4) {
    $consonants .= '0123456789';
  }
  if ($strength & 8) {
    $consonants .= '@#$%^';
  }
  $password = '';
  $alt = time() % 2;
  srand(time());
  for ($i = 0; $i < $length; $i++) {
    if ($alt == 1) {
      $password .= $consonants[(rand() % strlen($consonants))];
      $alt = 0;
    } else {
        $password .= $vowels[(rand() % strlen($vowels))];
      $alt = 1;
    }
  }
  return $password;
}

Discussion:

After seeing the comments regarding password security I amended Shane's original code to include a strength option. This is a bit mask of the various options: 1 adds in upper case consonants, 2 adds in upper case vowels, 4 adds in numbers and 8 adds in special characters.
make_password(8,3); would geberate an 8 character password with upper and lower consonants and vowels.
make_password(8,5); would generate an 8 character password with upper case consonants and numbers.
It can still generate a valid dictionary entry at random (unless numbers and special characters are included)



Add comment

Number of comments: 1

better time resolution gives better randomness, Gareth Palidwor, 2004/10/04
Minor tweak:
list($microtime,$fulltime) = split("\s+",microtime()); $microtime = $microtime * 100000000; srand($microtime); $alt = $microtime % 2; With the settings in the script as is, it will give the same password every time it is called during the same second. Minor security hole. Using the microsecond resolution time to seed the random number is better.
Add comment



Highest rated recipes:

1. DB_eSession PHP class ...

2. main - python-like if ...

3. iPHP: Semi-interactive ...

4. tinySendMail

5. Microsoft Access ...

6. Single Linked List

7. PHP MySQL Search Class

8. Pass Javascript arrays ...

9. Microsoft Access ...

10. Password generation




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.