johnmahugu

php - random readable password

Jun 14th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1.  
  2.  
  3. function random_readable_pwd($length=10){
  4.  
  5.     // the wordlist from which the password gets generated
  6.     // (change them as you like)
  7.     $words = 'dog,cat,sheep,sun,sky,red,ball,happy,ice,';
  8.     $words .= 'green,blue,music,movies,radio,green,turbo,';
  9.     $words .= 'mouse,computer,paper,water,fire,storm,chicken,';
  10.     $words .= 'boot,freedom,white,nice,player,small,eyes,';
  11.     $words .= 'path,kid,box,black,flower,ping,pong,smile,';
  12.     $words .= 'coffee,colors,rainbow,plus,king,tv,ring';
  13.  
  14.     // Split by ",":
  15.     $words = explode(',', $words);
  16.     if (count($words) == 0){ die('Wordlist is empty!'); }
  17.  
  18.     // Add words while password is smaller than the given length
  19.     $pwd = '';
  20.     while (strlen($pwd) < $length){
  21.         $r = mt_rand(0, count($words)-1);
  22.         $pwd .= $words[$r];
  23.     }
  24.  
  25.     // append a number at the end if length > 2 and
  26.     // reduce the password size to $length
  27.     $num = mt_rand(1, 99);
  28.     if ($length > 2){
  29.         $pwd = substr($pwd,0,$length-strlen($num)).$num;
  30.     } else {
  31.         $pwd = substr($pwd, 0, $length);
  32.     }
  33.  
  34.     return $pwd;
  35.  
  36. }
  37. /**
  38.  
  39. Snippet Details
  40.  
  41.     Author
  42.     John Mahugu
  43.     License
  44.     Public Domain
  45.     Language
  46.     PHP
  47.     Created
  48.     04/25/2006
  49.     Updated
  50.     03/11/2011
  51.     Tags
  52.     string functions, random
  53.  
  54. */
  55.  
  56. //example
  57. //random_readable_pwd(10) => returns something like: pingwater6, radiohap28, sunwhite84, happykid44, etc...
Advertisement
Add Comment
Please, Sign In to add comment