Location: PHPKode > scripts > Password Generator & Checker > password-generator-checker/password.class.php
<?php
/**
 * Class:		Password
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author 		Roomet Kirotarp <hide@address.com>
 * @package 	Security
 * @subpackage 	Password
 * @version 	1.0
 * @copyright 	2007 Roomet Kirotarp
 */


/**
 * Password management class
 * 
 * Class to manage passwords.
 *  
 * With this class You can:
 * - validate password strength
 * - generate password
 * 
 * Example code:
 * 
 * <code>
 * $PWD = & new Password();
 * $myPass = $PWD->generate(); // result: m4qci6QW
 * 
 * $PWD->set('uppercount', 0); 
 * $myPass = $PWD->getNew(); // result: e3wdf2qi
 * 
 * if($PWD->isSecure($myPass)){
 * 	   echo 'Password is not secure...';
 * } else {
 * 	   echo 'Password is secure...';
 * }
 * </code>
 *
 * @author 	Roomet Kirotarp <hide@address.com>
 * @package Password
 * @version 1.0
 */
class PassMan {

	/**
	 * Parameters array
	 * 
	 * Contains values:
	 * - length 		- password length
	 * - uppercount		- count of uppercase chars
	 * - digitcount		- count of digits
	 * - symbolcount	- count of predefined symbols
	 *
	 * @var array
	 */
	var $params = array();

	/**
	 * Class constructor
	 * 
	 * Sets default values for password generator and initializes random
	 * generator.
	 *
	 * @return boolean
	 */
	function PassMan() {
		// Constructor

		// Lets set up default parameters for password generator.
		$this->params['length'] = 8;
		$this->params['uppercount'] = 2;	// How many upper chars should be
		$this->params['digitcount'] = 2;	// How many digits should be
		$this->params['symbolcount'] = 0;	// How many symbols should be in password

		mt_srand($this->seeder());

		return true;
	}

	/**
	 * Password validator
	 *
	 * Checks that password is secure or not.
	 * Secure password :
	 * - has at least one uppercase char (A-Z)
	 * - has at least one lowercase char (a-z)
	 * - has at least one digit (0-9)
	 * - has length at least 6 letters.
	 *
	 * @param unknown_type $pass
	 * @return unknown
	 */
	function isSecure($pass){
		// validate pass
		preg_match_all('/([A-Z])/', $pass, $match);
		$res[] = count($match[1]);
		preg_match_all('/([a-z])/', $pass, $match);
		$res[] = count($match[1]);
		preg_match_all('/([0-9])/', $pass, $match);
		$res[] = count($match[1]);
		$res[] = strlen($pass);
		if(!empty($res[0]) && !empty($res[1]) && !empty($res[2]) && $res[3]>=8) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Password generator
	 * 
	 * Generates password. Default password generator values are
	 * defined in constructor function.
	 * 
	 * You can tell how many upper- or lowercase symbols will be
	 * in password or how many digits or other symbols.
	 * 
	 * <b>Note:</b> Every password will have at least one lowercase char
	 * to get mixed symbols in passwords always.
	 * 
	 * From alphabet are cut out symbols what may be confusing to
	 * read or undestand with some font styles.
	 *
	 * @return string
	 */
	function generate(){
		$char[] = 'abcdefghijkmnopqrstuvwxyz'; 	// Without l
		$char[] = 'ABCDEFGHJKLMNPQRSTUVWXYZ'; 	// Without I & O
		$char[] = '23456789';					// Without 0 & 1
		$char[] = '@#$%&-_!';					// Other symbols

		$password = '';
		$rest = array();
		// lets fill
		for($i=0; $i<$this->params['length']; $i++){
			$rest[] = $i;
			$x = mt_rand(0, strlen($char[0])-1);
			$password .= $char[0]{$x};
		}

		// Lets add some uppercase chars
		if($this->params['uppercount']>0 && count($rest)>0){
			$N = min($this->params['uppercount'], count($rest)-1);
			for ($i=0; $i<$N; $i++){
				$ch = mt_rand(0, count($rest)-1);
				$x = mt_rand(0, strlen($char[1])-1);
				$password{$rest[$ch]} = $char[1]{$x};
				unset($rest[$ch]);
				$rest = array_values($rest);
			}
		}

		// Lets add some uppercase chars
		if($this->params['digitcount']>0 && count($rest)>0){
			$N = min($this->params['digitcount'], count($rest)-1);
			for ($i=0; $i<$N; $i++){
				$ch = mt_rand(0, count($rest)-1);
				$x = mt_rand(0, strlen($char[2])-1);
				$password{$rest[$ch]} = $char[2]{$x};
				unset($rest[$ch]);
				$rest = array_values($rest);
			}
		}

		// Lets add some uppercase chars
		if($this->params['symbolcount']>0 && count($rest)>0){
			$N = min($this->params['symbolcount'], count($rest)-1);
			for ($i=0; $i<$N; $i++){
				$ch = mt_rand(0, count($rest)-1);
				$x = mt_rand(0, strlen($char[3])-1);
				$password{$rest[$ch]} = $char[3]{$x};
				unset($rest[$ch]);
				$rest = array_values($rest);
			}
		}

		return $password;

	}

	/**
	 * Password generator
	 * 
	 * Alias for generate() method.
	 *
	 * @return string
	 */
	function getNew(){
		return $this->generate();
	}

	/**
	 * Sets property value.
	 * 
	 * Gives to property new value
	 *
	 * @param string $param	Property name
	 * @param mixed $value	Property value
	 */
	function set($param, $value){
		$this->params[strtolower($param)] = $value;
	}

	/**
	 * Gets property value
	 *
	 * @param string $param Parameter name
	 * @return mixed
	 */
	function get($param){
		if(isset($this->params[strtolower($param)])){
			return $this->params[strtolower($param)];
		} else
			return NULL;
	}

	/**
	 * Random seed
	 *
	 * @return float
	 */
	function seeder(){
	  list($usec, $sec) = explode(' ', microtime());
	  return (float) $sec + ((float) $usec * 100000);
	}

}

?>
Return current item: Password Generator & Checker