password.class.php
Usage Example
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(128, 255);
$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);
}
}
?>
PHP Code:
<?php
require_once "password.class.php";
$password = new PasswordCreator();
echo $password->getPassword(64, PC_MINUS)."\n";
echo $password->getPassword(64, PC_MINUS | PC_CAPITAL)."\n";
echo $password->getPassword(64, PC_MINUS | PC_CAPITAL | PC_NUMBERS)."\n";
echo $password->getPassword(64, PC_MINUS | PC_CAPITAL | PC_NUMBERS | PC_SPACES)."\n";
echo $password->getPassword(64, PC_MINUS | PC_CAPITAL | PC_NUMBERS | PC_SPACES | PC_EXTENDED)."\n";
echo "\n";
?>


