generate random password
Moderator: General Moderators
generate random password
Hello everybody!
How do I generate a random password?
Thanks,
Rob.
How do I generate a random password?
Thanks,
Rob.
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
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.
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
http://www.php.net/manual/en/function.mt-rand.php -- Random FunctionDobson wrote:Hello everybody!
How do I generate a random password?
Code: Select all
$password = mt_rand();-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
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.
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.
Code: Select all
$chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
$password = '';
for($i=0; $i <= 15; $i++)
{
$char = mt_rand(0, strlen($chars)-1);
$password .= $chars{$char};
}
echo $password;* 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.
Last edited by santosj on Sat Apr 29, 2006 8:44 pm, edited 1 time in total.
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
and here is the code..feel free to modify. it can be even more secure 
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);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;
}- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact:
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;
}