NamePros.Com (http://www.namepros.com/)
-   CODE (http://www.namepros.com/code/)
-   -   Random Passwords (http://www.namepros.com/code/391711-random-passwords.html)

SecondVersion 11-03-2007 11:36 AM

Random Passwords
 
Got a tad bored today, so wrote a function for random passwords. Maybe it will help someone. :tu:

PHP Code:
<?php

/**
* Generate a random password.
*
* @param  integer  $numchars      How long do we need the password to be?
* @param  boolean  $specialchars  Include the special characters?
* @param  boolean  $extrashuffle  Include an extra randomization on the password string?
* @return string
*/
function random_pass($numchars = 8, $specialchars = true, $extrashuffle = false)
{
    
$numchars = intval($numchars);
    
$numchars = ($numchars > 16 OR $numchars < 8) ? 8 : $numchars;

    
$chars = array_merge(range('a', 'z'), range(0, 9));

    if (
$specialchars)
    {
        
$chars = array_merge($chars, array('!', '$', '_', '-', '#', '@'));
    }
    
shuffle($chars);

    
$pass = '';

    for (
$i = 0; $i <= $numchars; $i++)
    {
        
$pass .= $chars[$i];
    }

    if (
$extrashuffle)
    {
        return
str_shuffle($pass);
    }
    return
$pass;
}

// Example, returns: 3ck#4sib2
echo random_pass(8, true, true);

?>

Daniel 11-03-2007 12:31 PM

PHP Code:
<?php

/**
* Generate a random password.
*
* @param  integer  $numchars      How long do we need the password to be?
* @param  boolean  $specialchars  Include the special characters?
* @param  boolean  $extrashuffle  Include an extra randomization on the password string?
* @param  boolean  $mixedcase     Mixed case or solely lowercase?
* @return string
*/
function random_pass($numchars = 8, $specialchars = true, $extrashuffle = false, $mixedcase = true)
{
    
$numchars = intval($numchars);
    
$numchars = ($numchars > 16 OR $numchars < 8) ? 8 : $numchars;
    
    
$chars = array_merge(range('a', 'z'), range(0, 9));

    if (
$mixedcase) /* Extra case */
    
{
        
$chars = array_merge($chars, range('A','Z'));
    }
    
    if (
$specialchars)
    {
        
$chars = array_merge($chars, array('!', '$', '_', '-', '#', '@'));
    }
    
shuffle($chars);

    
$pass = '';

    for (
$i = 0; $i < $numchars; $i++) /* Changed to < only sign, otherwise it would be 1 character longer than needed */
    
{
        
$pass .= $chars[$i];
        
shuffle($chars); /* Get repeated characters, would add as an option, but I'm lazy */
    
}

    if (
$extrashuffle)
    {
        return
str_shuffle($pass);
    }
    return
$pass;
}

// Example, returns: 3ck#4si2
echo random_pass(8, true, true, true);

?>


Added an extra shuffle after each character, so you can get repeated characters too. (I prefer it that way).
Added MiXeD case option
Fixed the length ($i = 0; $i <=$numchars) part.

Barrucadu 11-04-2007 09:22 AM

Both good, but heres a function I made a while ago and have been using, it may not be as customizable, but the passwords are very difficult to guess:
PHP Code:
<?php
function prand($len = 10){
     
$pass = '';
     for(
$i = 0; $i < $len; $i ++){
          
$pass .= chr(rand(33, 126));
     }
     return
$pass;
}

echo
prand();
?>

jimmysanders 02-05-2008 03:42 PM

i had to go for the smallest one :)

PHP Code:
function randomPassword($length = 8) {
    return    
substr(base64_encode(md5(time())), 0, $length);
}



creates random string of A–Z, a–z, and 0–9 of max of 42ish chars

unknowngiver 02-18-2008 05:53 PM

Originally Posted by jimmysanders
i had to go for the smallest one :)

PHP Code:
function randomPassword($length = 8) {
    return    
substr(base64_encode(md5(time())), 0, $length);
}



creates random string of A–Z, a–z, and 0–9 of max of 42ish chars


lol protection much...base64 and md5 encryption lol

Daniel 02-19-2008 02:24 AM

Originally Posted by unknowngiver
Originally Posted by jimmysanders
i had to go for the smallest one :)

PHP Code:
function randomPassword($length = 8) {
    return    
substr(base64_encode(md5(time())), 0, $length);
}



creates random string of A–Z, a–z, and 0–9 of max of 42ish chars


lol protection much...base64 and md5 encryption lol



Not very secure, if you knew say what time roughly they made it, and how long, it could easily be cracked.

Dan

mholt 07-10-2008 08:21 PM

If you know exactly what second after Unix Epoch they made that password, then yes... you could guess it...

Unless we add a little bit of salt :)

PHP Code:
// Modified function, original given by jimmysanders

function randomPassword($length = 8) {
    
$saltLength = 1;
    return    
substr(base64_encode(md5(time())), 0, $length - $saltLength) . substr(md5($length + $time), 5, $saltLength);

// To use for your own, change:
// 5 on the second substr() function such that it's > 0 and < 42.
// $saltLength should be < $length
// If each person who used this function changed these values to
// some of their own choice, guessing it by just time() would be much trickier.
}


That should be a little trickier.


All times are GMT -7. The time now is 10:13 PM.
Site Sponsors
Advertise your business at NamePros

Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0