PwdGen v0.2

Published on 2007-11-22 01:23:08.

tags = {  "math" "crypto" "fun" "php"   };

bookmarks = {  Digg! , Del.icio.us! , Google! , Slashdot! , Netscape! , Technorati! , Yahoo! , Stumbleupon! };

Description

Pseudo-random password generator written in PHP.

Content

   1  
   2  #!/usr/bin/php
   3  <?php
   4  /* $Id: pwdgen.php 38 2007-07-13 23:07:59Z zapotek $ */
   5  /********************************************************************
   6   * Author Info
   7   * -----------
   8   * Author:       Zapotek
   9   * E-Mail:       zapotek@segfault.gr
  10   * Website:      http://www.segfault.gr
  11   *
  12   * File Info
  13   * ---------
  14   * Name:         PwdGen
  15   *
  16   * Comments
  17   * --------
  18   * Just a pseudo-random password generator
  19   * *******************************************************************/

  20  
  21  define( 'VERSION', 0.2 );
  22  
  23  define( 'NUMERIC_START', ord( 0 ) );
  24  define( 'NUMERIC_END', ord( 9 ) );
  25  
  26  define( 'CAPS_START', ord( 'A' ) );
  27  define( 'CAPS_END', ord( 'Z' ) );
  28  
  29  define( 'LETTERS_START', ord( 'a' ) );
  30  define( 'LETTERS_END', ord( 'z' ) );
  31  
  32  define( 'ALL_START', ord( ' ' ) );
  33  define( 'ALL_END', ord( '~' ) );
  34  
  35  $len       = $argv[1];
  36  $char_type = $argv[2];
  37  
  38  echo "\nPwdGen v". VERSION .
  39       "\nPseudo-random password generator\n" .
  40       "by Zapotek <zapotek [at] segfault.gr>\n" .
  41       "<http://www.segfault.gr>\n\n";
  42           
  43  if( $argc < 3 ) {
  44      echo "Usage: $argv[0] <length> <type>\n\n".
  45           "Types:\n".
  46           "-num\tNumerical\n".
  47           "-ll\tLower letters\n".
  48           "-hl\tCapital letters\n".
  49           "-e\tEverything goes\n";
  50           exit( );
  51  }
  52  
  53  switch( $argv[2] ) {
  54  
  55      case "-num":
  56          for( $i = 0; $i < $len; $i ++ ) {
  57              $out  = rand( NUMERIC_START, NUMERIC_END );
  58              $pwd .= sprintf( "%c", $out );
  59          }
  60          $combos = get_combos( NUMERIC_START, NUMERIC_END, $len );
  61         
  62          break;
  63  
  64      case "-ll":
  65          for( $i = 0; $i < $len; $i ++ ) {
  66              $out  = rand( LETTERS_START, LETTERS_END );
  67              $pwd .= sprintf("%c", $out);
  68          }
  69          $combos = get_combos( LETTERS_START, LETTERS_END, $len );
  70         
  71          break;
  72  
  73      case "-hl":
  74          for( $i = 0; $i < $len; $i ++ ) {
  75              $out  = rand( CAPS_START, CAPS_END );
  76              $pwd .= sprintf( "%c", $out );
  77          }
  78          $combos = get_combos( CAPS_START, CAPS_END, $len );
  79         
  80          break;
  81  
  82      case "-e":
  83          for( $i = 0; $i < $len; $i ++ ) {
  84              $out  = rand( ALL_START, ALL_END );
  85              $pwd .= sprintf( "%c", $out );
  86          }
  87          $combos = get_combos( ALL_START, ALL_END, $len );
  88         
  89          break;
  90         
  91      default:
  92          echo "Unsupported character type set.\n";
  93          exit;
  94          break;
  95  
  96  }
  97  
  98  echo "Generated password: " . $pwd;
  99  echo "\n\n( $combos possible combinations)\n\n";
 100  
 101  /**
 102   * get the number of possible strings
 103   *
 104   * @param    int    ASCII value of first character in range
 105   * @param    int    ASCII value of last character in range
 106   * @param    int    the length of the string
 107   *
 108   * @return   int    the number of possible character combinations
 109   *
 110   */

 111  function get_combos( $start, $end, $len ) {
 112      return pow( $end - $start, $len );
 113  }
 114  
 115  ?>
 116  
 117   
Code statistics
Physical lines Code lines Comment lines Empty lines Size
116 [ 100.00% ] 67 [ 57.76% ] 24 [ 20.69% ] 25 [ 21.55% ] 2845 bytes
[ Download ]