Re: creating a alphanumeric password
| From: | Evan Nemerson | Date: | Sun, 18 May 2003 21:48:02 +0000 |
| Subject: | Re: creating a alphanumeric password | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148124@lists.php.net to get a copy of this message | ||
They're all over the internet. www.google.com. anyways here's one I wrote
which is pretty useful. Handles variable lengths, alpha-numeric, and
pronouncable. I really just wrote it for me, so it's not too clean, but here
it is anyways.
#!/usr/local/bin/php -q
<?php
function genpropwd($length)
{
$vowels = array("a","e","i","o","u");
$cons =
array("b","c","d","g","h","j","k","l","m","n","p","r","s","t","u","v","w","tr","cr","br","fr","th","dr","ch","ph","wr","st","sp","sw","pr","sl","cl");
$num_vowels = count($vowels);
$num_cons = count($cons);
$password = '';
for ( $i=0 ; $i<$length ; $i++ )
$password .= $cons[mt_rand(0, $num_cons-1)].$vowels[mt_rand(0,
$num_vowels-1)];
return substr($password,0,$length);
}
function genpwd($length)
{
$pwd = '';
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
for ( $x=0 ; $x<$length ; $x++ )
{
$tmp = substr($chars,mt_rand(0,strlen($chars)-1),1);
if ( mt_rand(0,1) == 1 )
$tmp = strtoupper($tmp);
$pwd .= $tmp;
}
return $pwd;
}
function writeHelp()
{
echo "USAGE: ".$_SERVER['argv'][0]." [OPTIONS]\n".
"OPTIONS:\n".
" -l:INTL,--length:INT\tGenerate passwords of length INTL.\n".
" \t\t\tThe default value is twelve (12) characters.\n".
" -c:INTC,--count:INT\tGenerate INTC passwords.\n".
" \t\t\tThe default value is one (1) password.\n".
" -p,--pronounceable\tGenerate a pronouncable password.\n".
" -h,--help\t\tGenerate this help screen.\n";
exit();
}
$myArgs = $_SERVER['argv'];
unset($myArgs[0]);
$length = 12;
$p = $number = FALSE;
foreach ( $myArgs as $arg )
{
if ( ($arg == '-h') | ($arg == '--help') )
writeHelp();
elseif ( ( substr($arg,0,3) == '-c:' ) )
$count = substr($arg,3);
elseif ( ( substr($arg,0,8) == '--count:' ) )
$count = substr($arg,8);
elseif ( substr($arg,0,9) == '--length:' )
$length = substr($arg,9);
elseif ( substr($arg,0,3) == '-l:' )
$length = substr($arg,3);
elseif ( ($arg == '-p' ) | ($arg == '--pronounceable'))
$p = TRUE;
elseif ( ($arg == '-n' ) | ($arg == '--number-lines'))
$number = TRUE;
}
$x=$y=0;
if ( !isset($count) )
$count = 1;
switch($p)
{
case TRUE:
for ( $x=0 ; $x<$count ; $x++ )
{
if ( $number == TRUE )
echo str_pad(++$y,4,' ',STR_PAD_LEFT).') ';
echo genpropwd($length)."\n";
}
break;
case FALSE:
for ( $x=0 ; $x<$count ; $x++ )
{
if ( $number == TRUE )
echo str_pad(++$y,4,' ',STR_PAD_LEFT).') ';
echo genpwd($length)."\n";
}
break;
}
?>