PHP: Random Password Oneliner

I am exceedingly lazy, so I made a one-liner random password generator.

How it works: Creates an md5 from a random string, packs this into binary representation, base64 encodes that, strips some of the characters used by base64 (most importantly the = that are typically at the end), and then limits it to 8 characters. It’s been broken out here on several lines to make modifying it a bit easier.

(Could make longer passwords by changing what’s allowed and providing more text to base64_encode or hexadecimal to pack.)

$randpw = substr(
   preg_replace('/[^a-zA-Z0-9\.\-!\$]/', '',
      base64_encode(
         pack('H*', md5(time().rand()))
      )
   ), 0, 8);

One Response to “PHP: Random Password Oneliner”