reenadak

random password generator

Feb 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1.    
  2.     /*
  3.     - Random Password Generator
  4.     - Generates a password at random given specified limitations.
  5.    
  6.     $length               - The amount of characters the password should have.
  7.     $useCapitalLetters    - Should the password consist of capital letters?
  8.     $useNumbers           - Should the password consist of numbers?
  9.     $useSpecialCharacters - Should the password consist of special characters?
  10.     */
  11.    
  12.     public function randomPassword($length = 7, $useCapitalLetters = true, $useNumbers = true, $useSpecialCharacters = true) {
  13.         $chars = array(
  14.             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  15.             'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  16.             'y', 'z');
  17.        
  18.         $specialChars = array(
  19.             '!', '%', '^', '&', '*', '(', ')', '@');
  20.                
  21.         $pass = '';
  22.        
  23.         for ($i = 0; $i < $length; $i++) {
  24.             $rand = rand(0, 3);
  25.            
  26.             switch ($rand) {
  27.             case 0:
  28.                 $pass .= $chars[rand(0, count($chars) - 1)];
  29.                 break;
  30.             case 1:
  31.                 if ($useCapitalLetters)
  32.                     $pass .= strtoupper($chars[rand(0, count($chars) - 1)]);
  33.                 else
  34.                     $pass .= $chars[rand(0, count($chars) - 1)];
  35.                
  36.                 break;
  37.             case 2:
  38.                 if ($useNumbers)
  39.                     $pass .= rand(0, 9);
  40.                 else
  41.                     $pass .= $chars[rand(0, count($chars) - 1)];
  42.                
  43.                 break;
  44.             case 3:
  45.                 if ($useSpecialCharacters)
  46.                     $pass .= $specialChars[rand(0, count($specialChars) - 1)];
  47.                 else
  48.                     $pass .= $chars[rand(0, count($chars) - 1)];
  49.                
  50.                 break;
  51.             }
  52.         }
  53.        
  54.         return $pass;
  55.     }
Add Comment
Please, Sign In to add comment