1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Password Creator: using bitwise operations

Discussion in 'PHP' started by extraweb, May 28, 2008.

  1. extraweb

    extraweb New Member

    Joined:
    May 28, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    web developer
    Location:
    kolkata
    password.class.php
    PHP:
    <?php
    /*
    * Simple password generator

    define ('PC_MINUS',    0x01);
    define ('PC_CAPITAL',  0x02);
    define ('PC_NUMBERS',  0x04);
    define ('PC_SPACES',   0x08);
    define ('PC_EXTENDED', 0x10);

    class PasswordCreator
    {
        /**
         * Contains the ASCII table representation
         *
         * @access private
         * @var array
         */
        
    protected $_ascii_table null;

        public function 
    __construct()
        {
            
    $this->populateAsciiTable();
        }

        
    /**
         * Gets a random password with the specified length and flags
         *
         * @param int $length
         * @param int $flags
         * @return string
         */
        
    public function getPassword($length 16$flags PC_MINUS)
        {
            
    /* If there is no length we throw an exception: */
            
    if ($length 1)
                throw new 
    Exception ("Password lenght must be at least 1 character lenght");

            
    /* Get the character matrix and its lenght: */
            
    $characters $this->getPasswordCharacters($flags);
            
    $characters_length count($characters);

            
    /* Build the password: */
            
    for ($i 0$i $length$i++) {
                
    $random rand(0$characters_length 1);
                
    $output .= $characters[$random];
            }

            
    /* Return it: */
            
    return $output;
        }

        
    /**
         * Gets the final character matrix with the specified flags.
         *
         * @param int $flags
         * @return array
         */
        
    protected function getPasswordCharacters($flags)
        {
            
    $chars_to_use  = array();

            if (
    $flags PC_MINUS) {
                
    $chars_minus   $this->getAsciiRange('a''z');
                
    $chars_to_use array_merge($chars_to_use$chars_minus);
            }

            if (
    $flags PC_CAPITAL) {
                
    $chars_mayus   $this->getAsciiRange('A''Z');
                
    $chars_to_use array_merge($chars_to_use$chars_mayus);
            }

            if (
    $flags PC_NUMBERS) {
                
    $chars_numbers $this->getAsciiRange(48,   57);
                
    $chars_to_use array_merge($chars_to_use$chars_numbers);
            }

            if (
    $flags PC_SPACES) {
                
    $chars_spaces  $this->getSpaces();
                
    $chars_to_use array_merge($chars_to_use$chars_spaces);
            }

            if (
    $flags PC_EXTENDED) {
                
    $chars_ext     $this->getAsciirange(128255);
                
    $chars_to_use  array_merge($chars_to_use$chars_ext);
            }

            return 
    $chars_to_use;
        }

        
    /**
         * Gets the ASCII characters between the specified range
         *
         * @param int $start First character
         * @param int $end Last character
         * @return array
         */
        
    protected function getAsciiRange($start$end)
        {
            
    $start is_numeric($start) ? $start ord($start);
            
    $end   is_numeric($end)   ? $end   ord($end);

            for (
    $i $start$i <= $end$i++)
                
    $output[] = $this->_ascii_table[$i];

            return 
    $output;
        }

        
    /**
         * Gets the space characters (tab, newline and space)
         *
         * @return array
         */
        
    protected function getSpaces()
        {
            
    $spaces = array("\t""\n"" ");

            foreach (
    $spaces as $space)
                
    $output[] = ord($space);

            return 
    $output;
        }

        
    /**
         * Populates the internal ASCII table
         */
        
    private function populateAsciiTable()
        {
            for (
    $i 0$i <= 255$i++)
                
    $this->_ascii_table[] = chr($i);
        }
    }
    ?>
    Usage Example
    PHP:
    <?php
    require_once "password.class.php";

    $password = new PasswordCreator();
    echo 
    $password->getPassword(64PC_MINUS)."\n";
    echo 
    $password->getPassword(64PC_MINUS PC_CAPITAL)."\n";
    echo 
    $password->getPassword(64PC_MINUS PC_CAPITAL PC_NUMBERS)."\n";
    echo 
    $password->getPassword(64PC_MINUS PC_CAPITAL PC_NUMBERS PC_SPACES)."\n";
    echo 
    $password->getPassword(64PC_MINUS PC_CAPITAL PC_NUMBERS PC_SPACES PC_EXTENDED)."\n";

    echo 
    "\n";
    ?>
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    How's this useful than other password generators?
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  4. ZERO.DEGREE

    ZERO.DEGREE New Member

    Joined:
    Jun 3, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    BURNING_WINÐ
    Humm sounds great
     
  5. Jenniferlinn

    Jenniferlinn New Member

    Joined:
    Dec 24, 2008
    Messages:
    23
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    search engine optimization
    Location:
    UK
    Home Page:
    Thanks for sharing the script, i will try it and then let you know about the feedback
     
  6. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    I used a html form that has a password field named password and a submit button named
    submit. When the submit button is clicked the page reloads with the $password variable sent
    back to the server. Then when php resends the html to the client it includes the appropriate
    javascript to redirect them to the correct protected page.
    Code:
    <?php
    if($password){
    if($password==coach){
    ?>
    <script language="JavaScript">
    location="http://www.charlottesoccer.com/coach.php3";
    </script>
    <?php
    }elseif ($password==admin){
    ?>
    <script language="JavaScript">
    location="http://www.charlottesoccer.com/admin.php3";
    </script>
    <?php
    }elseif ($password==member){
    ?>
    <script language="JavaScript">
    location="http://www.charlottesoccer.com/member.php3";
    </script>
    <?php
    }elseif ($password==guest){
    ?>
    <script language="JavaScript">
    location="http://www.charlottesoccer.com/guest.php3";
    </script>
    <?php
    }else{
    ?>
    <script language="JavaScript">
    location="http://www.charlottesoccer.com/signin.php3";
    </script>
    <?php
    }
    }
    ?>
    You see that as many passords as you need can be set up.
    If there are a large amount of passwords it would be better to set up a select case statement.
     

Share This Page