Zend - The PHP Company




Passwords

Add Code


Dual Mode Password Generation System  

Type: code fragment
Added by: David_H
Entered: 11/04/2003
Last modified: 05/12/2002
Rating: *** (3 votes)
Views: 5657
A function that can create random passwords, or passwords based on words from a file.


<?php
##Dual Mode Password Creation System 
##            by                     
##      David Hamilton                     
##Visit www.potentialcreations.com for more information
##This function provided without any warrenty expressed or implied.
##Designed as a learning tool only. Not intended for any 
#particular use.

function pass_gen($min$max,$filename,$type=1){
#Seeding the random number generator
#Required by some older versions of PHP
srand((float) microtime() * 1000000); 
#open a text file with one word per line, number of words is arbitray
#PHP is quick, I use a 1200+ word file as a test and the script still
#executes in less than .005 seconds

$fd=fopen($filename,"r") or die("Error accessing data file");
$fstring=fread($fd,filesize($filename));
$spice=array("0","1","2","3","4","5","6","8","9","#","&","@","$","_","%","?","+");
#################################################
############BIG WARNING#########################
#$wordl_list is an explode on n the Windows newline character,
#if your word file was made on another OS change the explode 
#character to the proper on for your OS 
$length=rand($min,$max);
$word_list=explode("n",$fstring);
$list_size=sizeof($word_list);
$spice_size=sizeof($spice);


if (
$type==1)
{


do
{
#Some word lists are "noisy" 
#and have spaces at the end of the words 
#So we trim em, and pick 2 random words.
$a=trim($word_list[rand(0,$list_size)]);
$b=trim($word_list[rand(0,$list_size)]);
#Also, we must pick a spice character.
$c=$spice[rand(0,$spice_size)];
#Now for the fun part. We flip a coin to
#see which word will get uppercased
$upper=rand(1,2);
if (
$upper==1)
{
$a=strtoupper($a);
}

if (
$upper==2)
{
$b=strtoupper($b);
}


##One last bit of chaos..

$dice=rand(1,3);
if (
$dice==1)
{
$pass=$c.$a.$spice[rand(0,$spice_size)].$b;
}

if (
$dice==2)
{
$pass=$a.$c.$b;
}
if (
$dice==3)
{
$pass=$a.$c.$b.$spice[rand(0,$spice_size)];

}
while (
strlen($pass)<$min)
     {
     
$pass.=$spice[rand(0,$spice_size)];
     }


}while (
strlen($pass)<$min || strlen($pass)>$max);
#We have a range of acceptable lengths, so if it falls out 
#of bounds we have to loop over.

return $pass;

}

##This is the type setting for giberish passwords
if ($type==2)
{

#This is a modified version of a tecnique other 
#password generation programs have used.
#Using array_merge to get a nice big set of letters and numbers.
$deck array_merge 
        

range("0""9"), 
range("a""z"), 
range("A""Z"),
array(
"0","1","2","3","4","5","6","8","9","#","&","@","$","_","%","?","+")

        );

#this do loop glues together discrete elements of the array ($deck)
#and merges them into a passowrd
do{

    for(
$i 0$i $length$i++) 
        {
      
$shuffle=rand(0,sizeof($deck));
        
$pass.=$deck[$shuffle]; 
                 
    }
        
}
#This is where we check to make sure the generated password
#Does not match one in our "no-no" list.
while(array_search($pass,$word_list));
#once done, we just return the password.
return $pass;
}
}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories