Guest User

Untitled

a guest
Oct 23rd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?PHP
  2. // Generates a strong password of N length containing at least one lower case letter,
  3. // one uppercase letter, one digit, and one special character. The remaining characters
  4. // in the password are chosen at random from those four sets.
  5. //
  6. // The available characters in each set are user friendly - there are no ambiguous
  7. // characters such as i, l, 1, o, 0, etc. This, coupled with the $add_dashes option,
  8. // makes it much easier for users to manually type or speak their passwords.
  9. //
  10. // Note: the $add_dashes option will increase the length of the password by
  11. // floor(sqrt(N)) characters.
  12.  
  13. function generateStrongPassword($length = 9, $add_dashes = false, $available_sets = 'luds')
  14. {
  15. $sets = array();
  16. if(strpos($available_sets, 'l') !== false)
  17. $sets[] = 'abcdefghjkmnpqrstuvwxyz';
  18. if(strpos($available_sets, 'u') !== false)
  19. $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
  20. if(strpos($available_sets, 'd') !== false)
  21. $sets[] = '23456789';
  22. if(strpos($available_sets, 's') !== false)
  23. $sets[] = '!@#$%&*?';
  24.  
  25. $all = '';
  26. $password = '';
  27. foreach($sets as $set)
  28. {
  29. $password .= $set[array_rand(str_split($set))];
  30. $all .= $set;
  31. }
  32.  
  33. $all = str_split($all);
  34. for($i = 0; $i < $length - count($sets); $i++)
  35. $password .= $all[array_rand($all)];
  36.  
  37. $password = str_shuffle($password);
  38.  
  39. if(!$add_dashes)
  40. return $password;
  41.  
  42. $dash_len = floor(sqrt($length));
  43. $dash_str = '';
  44. while(strlen($password) > $dash_len)
  45. {
  46. $dash_str .= substr($password, 0, $dash_len) . '-';
  47. $password = substr($password, $dash_len);
  48. }
  49. $dash_str .= $password;
  50. return $dash_str;
  51. }
Add Comment
Please, Sign In to add comment