Page 1 of 1

secure password generator function

Posted: Fri Mar 26, 2010 7:47 am
by s.dot
I'm not just posting this for something to post :P

I'm gonna start using it on every site I sign up on since browsers save passwords and it's easy to get new ones. Better than using the same password for every site like some people do ;)

Some people may find this useful.

Code: Select all

function genPass()
{
    $chars = array_merge(
        range('A', 'Z'),
        range('a', 'z'),
        range(0, 9),
        array(
            '!', '@', '#', '$', '%', '^', '*', '(', ')', '-', '_', '+', '-', '/', '\'', ';', ',', '<', '>', '"', '~', '`', '?'
        )
    );
    
    shuffle($chars);
    return substr(implode($chars), 0, mt_rand(10, 16));
}
When ran 10 times it'll produce random strings like:

Code: Select all

I4x@m~P-09z<
,l9n'oF$q2
Bgsvnd(LJm*K/
Rt"h)KOE8W>g!
THOEd^>~@U
Py$+U4~dZ2aQLD
Dm'V$sUGw9C
r-83sUm^2kyp
lyD4U7rv(BXx
*mk7U~0sqy)
Edit: In line 8, it is ' \ ' ', not ' ' '

Re: secure password generator function

Posted: Fri Mar 26, 2010 10:46 am
by flying_circus
It's nice :D
However, I just formatted my machine and had trouble remember the 1 password I use for every site :(

Re: secure password generator function

Posted: Fri Mar 26, 2010 11:07 am
by Benjamin
A single password for every site, and you didn't even have that password backed up!? Tsk Tsk.

Re: secure password generator function

Posted: Fri Mar 26, 2010 11:35 am
by s.dot
flying_circus wrote:It's nice :D
However, I just formatted my machine and had trouble remember the 1 password I use for every site :(
Very very bad! Considering a large portion of today's developers STILL don't even hash passwords.

Re: secure password generator function

Posted: Fri Mar 26, 2010 3:20 pm
by AbraCadaver
Just for fun:

Code: Select all

return implode(array_rand(array_flip(range('!', '~')), mt_rand(10, 16)));
 
// or
 
return implode(array_rand(array_fill_keys(range('!', '~'), null), mt_rand(10, 16)));

Re: secure password generator function

Posted: Fri Mar 26, 2010 9:06 pm
by s.dot
lmao nice, i didn't even know !-~ was a range

Re: secure password generator function

Posted: Fri Mar 26, 2010 10:20 pm
by AbraCadaver
s.dot wrote:lmao nice, i didn't even know !-~ was a range
Yeah, range() is very cool. Just look at the ascii table. Since cap letters come first, range('A', 'z') will give you all cap letters and all lowercase letters. You can even circle around.