FreeOZ论坛

标题: 新人献礼3:password generator [打印本页]

作者: 周星星1832    时间: 12-2-2009 13:30
标题: 新人献礼3:password generator
<?php
/************************************************************************/
/* Password.classphp                                                    */
/* =====================================================================*/
/* Copyright (c) 2008 by fumin lu                                       */
/*                                                                      */
/* Author(s): fumin lu                                                  */
/* =====================================================================*/
/* Title:                                                               */         
/*      Random Password Generating Class take some Conditions when      */
/*       generating the Password                                        */
/*   Date: Jan sec, 2008                                                */
/* =====================================================================*/

// Class Declaration Starts:
class Password{
   
    var $PasswordLength;    // Variable To Assign Password Length
    var $caps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var $small= 'abcdefghjkmnpqrstuvwxyz';
    var $nums = '0123456789';
    var $all  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz0123456789';
        var $condition;
    var $minLen;
   
    function Password($passLen,$condition)
    { //Constructor To Assign Values
        $this->condition = $condition;  // Will Store the Condition Array to Global Variable
        // Will calculate the Minimum Length
        $this->minLen = $this->condition['caps']+$this->condition['small']+$this->condition['nums'];
        // Compute the Total Password Length and Store it to the Global Variable
        $this->PasswordLength = max($this->minLen, $passLen);
        $this->condition['all']=$this->PasswordLength-$this->minLen;
    }
   
    function PassGen()
    {   
        $password_array_position=array();
        $password_array=array();
        for($i=1;$i<$this->PasswordLength+1;$i++)
        {
            $password_array_position[$i]=$i;
        }
      
        if($this->condition['caps']>0)
        {
      
            $this->get_chars('caps',$password_array,$password_array_position);
        }
        if($this->condition['small']>0)
        {
            $this->get_chars('small',$password_array,$password_array_position);
           
        }
        if($this->condition['nums']>0)
        {
           
            $this->get_chars('nums',$password_array,$password_array_position);
           
           
        }
      
        if($this->condition['all'])
        {
            $this->get_chars('all',$password_array,$password_array_position);
        }
        ksort($password_array);
        return str_shuffle(implode($password_array,''));
    }
   

    function get_chars($type,&$password_array,&$password_array_position)
    {
        for($i=0;$i<$this->condition[$type];$i++)
        {
            $position=array_rand($password_array_position,1);
            $password_array[$position]= $this->get_char($this->);
            $password_array_position=array_diff($password_array_position,array($position=>$position));
        }
           
    }

    function PassCheck($pass)
    {   // Function To Check Whether the Password have those Conditions
        $cond = array('caps'=>0, 'small'=>0, 'nums'=>0);
        for ($i=0;$i<strlen($pass);$i++)
        {
            $c = substr($pass, $i, 1);
            if (strpos($this->caps, $c)) { $cond['caps']++; }
            if (strpos($this->small, $c)) { $cond['small']++; }
            if (strpos($this->nums, $c)) { $cond['nums']++; }
           
        }
        if ($this->condition['caps']<=$cond['caps'] && $this->condition['small']<=$cond['small'] && $this->condition['nums']<=$cond['nums'] )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
   
    function get_char($set)
    {
      
        list($usec, $sec) = explode(' ', microtime());
        $seed= (float) $sec + ((float) $usec * 100000);
        mt_srand($seed);
        $num = mt_rand() % strlen($set);
        $char = substr(str_shuffle($set), $num, 1);
        return $char;
    }
}
?>




欢迎光临 FreeOZ论坛 (https://www.freeoz.org/bbs/) Powered by Discuz! X3.2