PHP生成随机密码类分享


Posted in PHP onJune 25, 2014

类代码:

<?php
/**
 * PHP - Password Generator Class
 * Version 1.0.0
 *
 */
 
if (@!is_object($passGen) || !isset($passGen)) {
  $passGen = new Password;
}
 
class Password
{
 
  /**
   * 大写字母 A-Z
   *
   * @var array
   */
  protected $uppercase_chars;
 
  /**
   * 小写字母 a-z
   *
   * @var array
   */
  protected $lowercase_chars;
 
  /**
   * 阿拉伯数字 0-9
   *
   * @var array
   */
  protected $number_chars;
 
  /**
   * 特殊字符
   *
   * @var array
   */
  protected $special_chars;
 
  /**
   * 其他特殊字符
   *
   * @var array
   */
  protected $extra_chars;
 
  /**
   * 最终用来生成密码的所有字符
   *
   * @var array
   */
  protected $chars = array();
 
  /**
   * 密码长度
   *
   * @var array
   */
  public $length;
 
  /**
   * 是否使用大写字母
   *
   * @var boolean
   */
  public $uppercase;
 
  /**
   * 是否使用小写字母
   *
   * @var boolean
   */
  public $lowercase;
 
  /**
   * 是否使用阿拉伯数字
   *
   * @var boolean
   */
  public $number;
 
  /**
   * 是否使用特殊字符
   *
   * @var boolean
   */
  public $special;
 
  /**
   * 是否使用额外的特殊字符
   *
   * @var boolean
   */
  public $extra;
 
  /**
   * 初始化密码设置
   *
   * @param int $length
   */
  function Password($length = 12)
  {
    $this->length = $length;
     
    $this->configure(true, true, true, false, false);
  }
 
  /**
   * 配置
   */
  function configure($uppercase = false, $lowercase = false, $number = false,
            $special = false, $extra = false
  ) {
    $this->chars = array();
 
    $this->upper_chars  = array(
                 "A", "B", "C", "D", "E", "F", "G", "H", "I",
                 "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                 "S", "T", "U", "V", "W", "X", "Y", "Z"
                );
    $this->lower_chars  = array(
                 "a", "b", "c", "d", "e", "f", "g", "h", "i",
                 "j", "k", "l", "m", "n", "o", "p", "q", "r", 
                 "s", "t", "u", "v", "w", "x", "y", "z"
                );
    $this->number_chars = array(
                 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
                );
    $this->special_chars = array(
                 "!", "@", "#", "$", "%", "^", "&", "*", "(", ")"
                );
    $this->extra_chars  = array(
                 "[", "]", "{", "}", "-", "_", "+", "=", "<",
                 ">", "?", "/", "`", "~", "|", ",", ".", ";", ":"
                );
 
    if (($this->uppercase = $uppercase) === true) {
      $this->chars = array_merge($this->chars, $this->upper_chars);
    }
    if (($this->lowercase = $lowercase) === true) {
      $this->chars = array_merge($this->chars, $this->lower_chars);
    }
    if (($this->number = $number) === true) {
      $this->chars = array_merge($this->chars, $this->number_chars);
    }
    if (($this->special = $special) === true) {
      $this->chars = array_merge($this->chars, $this->special_chars);
    }
    if (($this->extra = $extra) === true) {
      $this->chars = array_merge($this->chars, $this->extra_chars);
    }
 
    $this->chars = array_unique($this->chars);
  }
   
  /**
   * 从字符列中生成随机密码
   *
   * @return string
   **/
  function generate()
  {
    if (empty($this->chars)) {
      return false;
    }
 
    $hash    = '';
    $totalChars = count($this->chars) - 1;
     
    for ($i = 0; $i < $this->length; $i++) {
      $hash .= $this->chars[$this->random(0, $totalChars)];
    }
 
    return $hash;
  }
 
  /**
   * 生成随机数字
   *
   * @return int
   */
  function random($min = 0, $max = 0)
  {
    $max_random = 4294967295;
 
    $random = uniqid(microtime() . mt_rand(), true);
    $random = sha1(md5($random));
 
    $value = substr($random, 0, 8);
    $value = abs(hexdec($value));
 
    if ($max != 0) {
      $value = $min + ($max - $min + 1) * $value / ($max_random + 1);
    }
 
    return abs(intval($value));
  }
}

调用:

<?php
 
include_once 'password.class.php';
 
echo $passGen->generate();
 
//FS4yq74e2LeE
PHP 相关文章推荐
PHP下编码转换函数mb_convert_encoding与iconv的使用说明
Dec 16 PHP
用php制作简单分页(从数据库读取记录)的方法详解
May 04 PHP
使用php显示搜索引擎来的关键词
Feb 13 PHP
PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
Jan 07 PHP
PHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间
May 06 PHP
PHP 将dataurl转成图片image方法总结
Oct 14 PHP
利用php_imagick实现复古效果的方法
Oct 18 PHP
使用PHP连接多种数据库的实现代码(mysql,access,sqlserver,Oracle)
Dec 21 PHP
PHP 配置后台登录以及模板引入
Jan 24 PHP
Yii2框架可逆加密简单实现方法
Aug 25 PHP
PHP使用ActiveMQ实现消息队列的方法详解
May 31 PHP
基于laravel belongsTo使用详解
Oct 18 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十二)
Jun 25 #PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十一)
Jun 25 #PHP
JavaScript创建命名空间的5种写法
Jun 24 #PHP
PHP获取windows登录用户名的方法
Jun 24 #PHP
PHP获取MySql新增记录ID值的3种方法
Jun 24 #PHP
PHP判断表单复选框选中状态完整例子
Jun 24 #PHP
PHP网页游戏学习之Xnova(ogame)源码解读(十)
Jun 24 #PHP
You might like
Zend Framework教程之Resource Autoloading用法实例
2016/03/08 PHP
Laravel框架生命周期与原理分析
2018/06/12 PHP
Yii框架日志操作图文与实例详解
2019/09/09 PHP
图片连续滚动代码[兼容IE/firefox]
2009/06/11 Javascript
查找Oracle高消耗语句的方法
2014/03/22 Javascript
JS中getYear()和getFullYear()区别分析
2014/07/04 Javascript
JS获取iframe中longdesc属性的方法
2015/04/01 Javascript
jquery实现鼠标经过显示下划线的渐变下拉菜单效果代码
2015/08/24 Javascript
JavaScript的模块化开发框架Sea.js上手指南
2016/05/12 Javascript
深入理解requestAnimationFrame的动画循环
2016/09/20 Javascript
解决vue 界面在苹果手机上滑动点击事件等卡顿问题
2018/11/27 Javascript
WebGL学习教程之Three.js学习笔记(第一篇)
2019/04/25 Javascript
微信小程序自定义多列选择器使用详解
2019/06/21 Javascript
python socket 超时设置 errno 10054
2014/07/01 Python
Python的time模块中的常用方法整理
2015/06/18 Python
Python中方法链的使用方法
2016/02/23 Python
python调用Matplotlib绘制分布点并且添加标签
2018/05/31 Python
python3实现表白神器
2019/04/09 Python
[机器视觉]使用python自动识别验证码详解
2019/05/16 Python
python与字符编码问题
2019/05/24 Python
Python基础学习之基本数据结构详解【数字、字符串、列表、元组、集合、字典】
2019/06/18 Python
python+selenium 点击单选框-radio的实现方法
2019/09/03 Python
详解python内置常用高阶函数(列出了5个常用的)
2020/02/21 Python
python小程序基于Jupyter实现天气查询的方法
2020/03/27 Python
马来西亚最热门的在线时尚商店:FashionValet
2018/11/11 全球购物
2014年上半年工作自我评价
2014/01/18 职场文书
英语老师推荐信
2014/02/26 职场文书
《囚绿记》教学反思
2014/03/01 职场文书
房屋买卖协议书范本
2014/04/10 职场文书
大学英语演讲稿范文
2014/04/24 职场文书
法定代表人资格证明书
2014/09/11 职场文书
党员领导干部民主生活会批评与自我批评发言
2014/09/28 职场文书
2015年毕业实习工作总结
2014/12/12 职场文书
学生会辞职信
2015/03/02 职场文书
学前教育见习总结
2015/06/23 职场文书
Redis 的查询很快的原因解析及Redis 如何保证查询的高效
2022/03/16 Redis