Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. <?php
  2. /*
  3. * Designed to be used on the command line.
  4. * php genpass.php [arg1] [arg2] ...
  5. * Accepts up to three parameters:
  6. * --length or -l = length of the password
  7. * --specialchars or -s = whether to include punctuation in generating the password
  8. * --unique or -u = whether to prevent repeated characters in a string
  9. *
  10. */
  11.  
  12.  
  13. $short = 'l::s::u::';
  14. $long = array('length::','special::','unique::');
  15.  
  16. $options = getopt($short, $long);
  17.  
  18. # Checks whether both --length and -l are set. If they're different, throw an error.
  19. if( isset( $options['length']) && isset( $options['l']) ):
  20. if( $options['length'] !== $options['l'] ):
  21. throw new Exception('Both -l and --length were set and they have different values. Please use just one.');
  22. endif;
  23. endif;
  24.  
  25. # Checks whether both --special and -u are set. If they're different, throw an error.
  26. if( isset( $options['special']) && isset( $options['s']) ):
  27. if( $options['s'] !== $options['special'] ):
  28. throw new Exception('Both -s and --special were set and they have different values. Please use just one.');
  29. endif;
  30. endif;
  31.  
  32. # Checks whether both --unique and -u are set. If they're different, throw an error.
  33. if( isset( $options['unique']) && isset( $options['u']) ):
  34. if( $options['unique'] !== $options['u'] ):
  35. throw new Exception('Both -u and --unique were set and they have different values. Please use just one.');
  36. endif;
  37. endif;
  38.  
  39. if( isset( $options['length']) ):
  40. $length = (int)$options['length'];
  41. elseif ( isset( $options['l']) ):
  42. $length = (int)$options['l'];
  43. endif;
  44.  
  45. if( isset( $options['special']) ):
  46. $specialchars = $options['special'];
  47. elseif ( isset( $options['s']) ):
  48. $specialchars = $options['s'];
  49. else:
  50. $specialchars = 'true';
  51. endif;
  52.  
  53. if( isset( $options['unique']) ):
  54. $unique = $options['unique'];
  55. elseif ( isset( $options['u']) ):
  56. $unique = $options['u'];
  57. else:
  58. $unique = 'false';
  59. endif;
  60.  
  61. function genpass($l=8, $sc='true', $u='false') {
  62. $password = '';
  63. $x = 0;
  64. $lower_alpha = range('a','z');
  65. $upper_alpha = range('A','Z');
  66. $digits = range(0,9);
  67. $punc = explode(',','!,@,#,$,%,^,&,*,(,),+,{,},[,],=,-,_');
  68.  
  69. if( $sc == 'true' ){
  70. $allowed = array_merge($lower_alpha, $upper_alpha, $digits, $punc);
  71. } else {
  72. $allowed = array_merge($lower_alpha, $upper_alpha, $digits);
  73. }
  74.  
  75. $allowed_length = count($allowed);
  76.  
  77. while($x < $l){
  78. $pick = rand(0,$allowed_length-1);
  79.  
  80. # This is a bug. It's not returning uniques.
  81. if($u ==='true'):
  82. # pick a unique character. array_slice returns an array. we need the first member.
  83. $password .= array_slice($allowed, $pick, 1, false)[0];
  84. else:
  85. $password .= $allowed[$pick];
  86. endif;
  87.  
  88. $x++;
  89. }
  90. return $password;
  91. }
  92.  
  93.  
  94. try{
  95. $password = genpass($length, $specialchars, $unique);
  96.  
  97. print PHP_EOL.PHP_EOL.PHP_EOL;
  98. print $password;
  99. print PHP_EOL.PHP_EOL.PHP_EOL;
  100.  
  101. } catch(Exception $e) {
  102.  
  103. print $e->getMessage();
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement