Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Password Creator: using bitwise operations (http://www.go4expert.com/articles/password-creator-using-bitwise-t10862/)

extraweb 28May2008 11:16

Password Creator: using bitwise operations
 
password.class.php
PHP Code:

<?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 Code:

<?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";
?>


pradeep 29May2008 11:53

Re: Password Creator: using bitwise operations
 
How's this useful than other password generators?

shabbir 2Jun2008 16:28

Re: Password Creator: using bitwise operations
 
Nomination for article of the month of May

ZERO.DEGREE 4Jun2008 01:55

Re: Password Creator: using bitwise operations
 
Humm sounds great

Jenniferlinn 31Dec2008 14:04

Re: Password Creator: using bitwise operations
 
Thanks for sharing the script, i will try it and then let you know about the feedback

gkumar 30Jun2009 15:14

Re: Password Creator: using bitwise operations
 
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.


All times are GMT +5.5. The time now is 23:14.