Page 1 of 2

generate random password

Posted: Sat Apr 29, 2006 8:23 pm
by Dobson
Hello everybody!

How do I generate a random password?

Thanks,
Rob.

Posted: Sat Apr 29, 2006 8:26 pm
by Charles256
i did this just the other day.
psuedo code.
generate random number- this number equals the number of charecters in password
do a for loop.
each iteration generate a new random number and based upon that use that number and transfer it into a charecter. the name of the function esdcapes me
but look up converting binary to letter or something along those lines. then append newly generated charecter to a variable that is steadily growing with each loop. tada.password made. If no one else relpies I'll post my code I have at the house when I get off in 2.5 hours.

Re: generate random password

Posted: Sat Apr 29, 2006 8:27 pm
by santosj
Dobson wrote:Hello everybody!

How do I generate a random password?
http://www.php.net/manual/en/function.mt-rand.php -- Random Function

Code: Select all

$password = mt_rand();

Posted: Sat Apr 29, 2006 8:29 pm
by Charles256
my way is better,more secure ;) mwua ha ha.

Posted: Sat Apr 29, 2006 8:36 pm
by Dobson
thanks for your help guys, it works!

Posted: Sat Apr 29, 2006 8:37 pm
by Charles256
dobson, for the record his way generates really really insecure passwords.just in case you care, if not, then rock on :-D

Posted: Sat Apr 29, 2006 8:38 pm
by santosj
Well, he asked for how to make a random password and I gave it. If the person asked for security, I would ask for 10 dollars.

Code: Select all

$chars = 'abcdefghijklmnopqrstuvwxyz1234567890';

$password = '';
for($i=0; $i <= 15; $i++)
{
	$char = mt_rand(0, strlen($chars)-1);
	$password .= $chars{$char};
}

echo $password;
The assumption however was made that I was just giving the correct example the first time. By applying the mt_rand() example, one would be able to create the next one. Given that this is also insecure, the next progression would to research how to make it better

* by scambling, the $chars string, and then running through it.
* By checking the last character and not repeating it.
* Keeping a list of all the randomized passwords and checking to make sure that none of them are repeated.

Posted: Sat Apr 29, 2006 8:43 pm
by Charles256
that's slightly better...damn..can't remember the name of the function. buddy.wait till I get home.granted my function is nothing special. I was just proud because I had never had to do one before.... Handled thousands of transactions before but the random password made me proud...fgirues :-D

Posted: Sat Apr 29, 2006 8:49 pm
by Dobson
It doesn't have to be too secure but please, if you can post your code when you get home that would be good. Thanks to both of you

Posted: Sat Apr 29, 2006 8:51 pm
by Charles256
santosj, sorry if it seemed like I was knocking you. :) Just some people are lazy and didn't want our OP to be too lazy. :) Nice edit BTW.

Posted: Sat Apr 29, 2006 11:34 pm
by Charles256
and here is the code..feel free to modify. it can be even more secure :-D

Code: Select all

$rand=rand(5,25);
        $new="";
        for ($i=0;$i<$rand;$i++)
        {
          $case=rand(0,1);
          if ($case=='0')
          {
            $letter=rand(65,90);
            $add=chr($letter);
            // uppercase letters
            $new=$new.$add;
          }
          else
          {
            $letter=rand(97,122);
            $add=chr($letter);
            //lowercase letters
            $new=$new.$add;
          }
        }
        $password=md5($new);

Posted: Sun Apr 30, 2006 8:36 am
by hawleyjr
Another:

Code: Select all

/*
	RETURN PASSWORD OF X AMOUNT OF LENGTH
*/
function getDefaultPassword( $pLen = 7 ){
	
	if( ! ( $pLen > 0 && $pLen < 20 ) )
		$pLen = 7;
	
	$str = strrev(substr(md5(microtime()),0,$pLen));
	if(!isSafeWord($str))
		getDefaultPassword();
	else
		return $str;

}

Posted: Mon May 01, 2006 2:48 am
by dibyendrah
what does the isSafeWord() functions does ?

Posted: Mon May 01, 2006 3:10 am
by Benjamin

Code: Select all

function GenerateRandomString($minlength, $maxlength, $uselower, $useupper, $usespecial, $usenumbers) {
  $charset = null;
  $key = null;
  if ($uselower)   $charset .= "abcdefghijklmnopqrstuvwxyz";
  if ($useupper)   $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  if ($usespecial) $charset .= "~@#$%^*()_+-={}|][";   // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./";
  if ($usenumbers) $charset .= "0123456789";
  if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength);
  else                         $length = mt_rand ($minlength, $maxlength);
  for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))];
  return $key;
}

Posted: Mon May 01, 2006 6:59 am
by hawleyjr
dibyendrah wrote:what does the isSafeWord() functions does ?
This is a random string which could (Very small chance) provide a word that has a curse or offensive word. The isSafeWord() functions just validates that there are no words from a list in the random string.