article

Random Password Generation Algorithm

Email
Submitted on: 1/1/2015 2:32:00 PM
By: Brian Griffin (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: PHP 4.0
Views: 4692
 
     This is a simple algorithm written for PHP4+ (Works with PHP3 with a few modifications) to randomly generate a password composed of 2 lower case characters, 2 upper case characters, and 2 integers.

 
				
/*
* RANDOM PASSWORD GENERATION ALGORITHM
* PROGRAMMED BY: BRIAN GRIFFIN
* January 1, 2003
* MXrider005@hotmail.com
*
* You can use this freely. Just don't credit it as your own work! And please e-mail me if you do just to let me know. Thanks.
*/
// DEFINE STRINGS TO USE FOR CHARACTER COMBINATIONS IN THE PASSWORD
$LCase = "abcdefghijklmnopqrstuvwxyz";
$UCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$Integer = "0123456789";

// DEFINE CONSTANTS FOR ALGORTTHM
define("LEN", "1");
/* THIS FUNCTION GENERATES A RANDOM NUMBER THAT WILL BE USED TO
* RANDOMLY SELECT CHARACTERS FROM THE STRINGS ABOVE
*/

function RndInt($Format){
switch ($Format){
case "letter":
$Rnd = rand(0,25);
if ($Rnd > 25){
$Rnd = $Rnd - 1;
}
break;
case "number":
$Rnd = rand(0,9);
if ($Rnd > 9){
$Rnd = $Rnd - 1;
}
break;
}
return $Rnd;
} // END RndInt() FUNCTION

/* RUN THE FUNCTION TO GENERATE RANDOM INTEGERS FOR EACH OF THE
* 6 CHARACTERS IN THE PASSWORD PRODUCED.
*/
$a = RndInt("letter");
$b = RndInt("letter");
$c = RndInt("letter");
$d = RndInt("letter");
$e = RndInt("number");
$f = RndInt("number");

// EXTRACT 6 CHARACTERS RANDOMLY FROM THE DEFINITION STRINGS
$L1 = substr($LCase, $a, LEN);
$L2 = substr($LCase, $b, LEN);
$U1 = substr($UCase, $c, LEN);
$U2 = substr($UCase, $d, LEN);
$I1 = substr($Integer, $e, LEN);
$I2 = substr($Integer, $f, LEN);

// COMBINE THE CHARACTERS AND DISPLAY THE NEW PASSWORD
$PW = $L1 . $U2 . $I1 . $L2 . $I2 . $U1;
echo("

The Password Is:\t$PW
");
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.