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 相关文章推荐
使用sockets:从新闻组中获取文章(二)
Oct 09 PHP
PHP ? EasyUI DataGrid 资料存的方式介绍
Nov 07 PHP
zend Framework中的Layout(模块化得布局)详解
Jun 28 PHP
php的GD库imagettftext函数解决中文乱码问题
Jan 24 PHP
WordPress中用于创建以及获取侧边栏的PHP函数讲解
Dec 29 PHP
PHP预定义变量9大超全局数组用法详解
Apr 23 PHP
php打乱数组二维数组多维数组的简单实例
Jun 17 PHP
删除PHP数组中头部、尾部、任意元素的实现代码
Apr 10 PHP
php实现和c#一致的DES加密解密实例
Jul 24 PHP
Ajax+PHP实现的分类列表框功能示例
Feb 11 PHP
PHP精确到毫秒秒杀倒计时实例详解
Mar 14 PHP
PHP+fiddler抓包采集微信文章阅读数点赞数的思路详解
Dec 20 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
php面向对象全攻略 (七) 继承性
2009/09/30 PHP
PHP中使用gettext解决国际化问题的例子(i18n)
2014/06/13 PHP
thinkphp ajaxfileupload实现异步上传图片的示例
2017/08/28 PHP
利用PHP获取汉字首字母并且分组排序详解
2017/10/22 PHP
PHP 实现base64编码文件上传出现问题详解
2020/09/01 PHP
extjs form textfield的隐藏方法
2008/12/29 Javascript
IE8 浏览器Cookie的处理
2009/01/31 Javascript
用jQuery扩展自写的 UI导航
2010/01/13 Javascript
js中将字符串转换成json的三种方式
2011/01/12 Javascript
js获取checkbox复选框选中的选项实例
2014/08/24 Javascript
Js+php实现异步拖拽上传文件
2015/06/23 Javascript
关于JS变量和作用域详解
2016/07/28 Javascript
BOM系列第二篇之定时器requestAnimationFrame
2016/08/17 Javascript
js获取时间函数及扩展函数的方法
2016/10/30 Javascript
bootstrapValidator自定验证方法写法
2016/12/01 Javascript
Bootstrap 中data-[*] 属性的整理
2018/03/13 Javascript
JS实现模糊查询带下拉匹配效果
2018/06/21 Javascript
在create-react-app中使用sass的方法示例
2018/10/01 Javascript
Vue实现Header渐隐渐现效果的实例代码
2020/11/05 Javascript
Vant+postcss-pxtorem 实现浏览器适配功能
2021/02/05 Javascript
深入理解Python中的super()方法
2017/11/20 Python
Django项目中用JS实现加载子页面并传值的方法
2018/05/28 Python
Python Numpy计算各类距离的方法
2019/07/05 Python
利用pytorch实现对CIFAR-10数据集的分类
2020/01/14 Python
python时间time模块处理大全
2020/10/25 Python
Origins悦木之源英国官网:雅诗兰黛集团高端植物护肤品牌
2017/11/06 全球购物
世界领先的艺术图书出版社:TASCHEN
2018/07/23 全球购物
澳大利亚天然护肤品、化妆品和健康产品一站式商店:Nourished Life
2018/12/02 全球购物
小学红领巾中秋节广播稿
2014/01/13 职场文书
工程专业求职自荐书范文
2014/02/08 职场文书
颁奖典礼主持词
2014/03/25 职场文书
初三学生评语大全
2014/04/24 职场文书
学校募捐倡议书
2014/05/14 职场文书
我的中国梦演讲稿高中篇
2014/08/19 职场文书
quickjs 封装 JavaScript 沙箱详情
2021/11/02 Javascript
根德5570型九灯四波段立体声收音机是电子管收音机的楷模 ? 再论5570
2022/04/05 无线电