secure password generator function

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

secure password generator function

Post 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 ' ' '
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: secure password generator function

Post 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 :(
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: secure password generator function

Post by Benjamin »

A single password for every site, and you didn't even have that password backed up!? Tsk Tsk.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: secure password generator function

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: secure password generator function

Post 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)));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: secure password generator function

Post by s.dot »

lmao nice, i didn't even know !-~ was a range
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: secure password generator function

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply