找回密码
 FreeOZ用户注册
查看: 1828|回复: 0
打印 上一主题 下一主题

[论坛技术] 新人献礼3:password generator

[复制链接]
跳转到指定楼层
1#
发表于 12-2-2009 13:30:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
<?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用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+10, 4-8-2026 07:08 , Processed in 0.023804 second(s), 17 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表