Advertisement
sanych_dv

Untitled

Jan 5th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1.  static public function generatePassword($length = 32, $add_dashes = false, $available_sets = 'lud')
  2.     {
  3.         $sets = array();
  4.         if (strpos($available_sets, 'l') !== false)
  5.             $sets[] = 'abcdefghjkmnpqrstuvwxyz';
  6.         if (strpos($available_sets, 'u') !== false)
  7.             $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
  8.         if (strpos($available_sets, 'd') !== false)
  9.             $sets[] = '123456789';
  10.         if (strpos($available_sets, 's') !== false)
  11.             $sets[] = '!@#$%&*?';
  12.  
  13.         $all = '';
  14.         $password = '';
  15.         foreach ($sets as $set) {
  16.             $password .= $set[array_rand(str_split($set))];
  17.             $all .= $set;
  18.         }
  19.  
  20.         $all = str_split($all);
  21.         for ($i = 0; $i < $length - count($sets); $i++)
  22.             $password .= $all[array_rand($all)];
  23.  
  24.         $password = str_shuffle($password);
  25.  
  26.         if (!$add_dashes)
  27.             return $password;
  28.  
  29.         $dash_len = floor(sqrt($length));
  30.         $dash_str = '';
  31.         while (strlen($password) > $dash_len) {
  32.             $dash_str .= substr($password, 0, $dash_len) . '-';
  33.             $password = substr($password, $dash_len);
  34.         }
  35.         $dash_str .= $password;
  36.         return $dash_str;
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement