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中的Class的几点个人看法
Oct 09 PHP
php中的一个中文字符串截取函数
Feb 14 PHP
PHP详细彻底学习Smarty
Mar 27 PHP
php简单对象与数组的转换函数代码(php多层数组和对象的转换)
May 18 PHP
PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数的小问题
May 29 PHP
PHP register_shutdown_function函数的深入解析
Jun 03 PHP
PHP动态页生成静态页的3种常用方法
Nov 13 PHP
thinkphp模板赋值与替换实例简述
Nov 24 PHP
Symfony2学习笔记之系统路由详解
Mar 17 PHP
关于php中一些字符串总结
May 05 PHP
Laravel 添加多语言提示信息的方法
Sep 29 PHP
laravel5.6中的外键约束示例
Oct 23 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脚本数据库功能详解(上)
2006/10/09 PHP
php求两个目录的相对路径示例(php获取相对路径)
2014/03/27 PHP
完美的2个php检测字符串是否是utf-8编码函数分享
2014/07/28 PHP
php计算两个文件相对路径的方法
2015/03/14 PHP
PHP基于面向对象实现的留言本功能实例
2018/04/04 PHP
PHP命名空间简单用法示例
2018/12/28 PHP
利用JQuery和JS实现奇偶行背景颜色自定义效果
2012/11/19 Javascript
jquery表格内容筛选实现思路及代码
2013/04/16 Javascript
sencha touch 模仿tabpanel导航栏TabBar的实例代码
2013/10/24 Javascript
js自动生成的元素与页面原有元素发生堆叠的解决方法
2014/09/04 Javascript
利用js实现禁止复制文本信息
2015/06/03 Javascript
浅谈Javascript线程及定时机制
2015/07/02 Javascript
深入理解JavaScript中的for循环
2017/02/07 Javascript
解决vue中对象属性改变视图不更新的问题
2018/02/23 Javascript
vue兄弟组件传递数据的实例
2018/09/06 Javascript
js针对图片加载失败的处理方法分析
2019/08/24 Javascript
JS实现盒子拖拽效果
2020/02/06 Javascript
[02:17]2016完美“圣”典风云人物:Sccc专访
2016/12/03 DOTA
[48:56]2018DOTA2亚洲邀请赛 3.31 小组赛 A组 VG vs KG
2018/03/31 DOTA
Python 的内置字符串方法小结
2016/03/15 Python
Python简单的制作图片验证码实例
2017/05/31 Python
python机器学习实战之最近邻kNN分类器
2017/12/20 Python
python 绘制拟合曲线并加指定点标识的实现
2019/07/10 Python
Python使用scrapy爬取阳光热线问政平台过程解析
2019/08/14 Python
Python 寻找局部最高点的实现
2019/12/05 Python
Python列表倒序输出及其效率详解
2020/03/04 Python
pycharm不以pytest方式运行,想要切换回普通模式运行的操作
2020/09/01 Python
HTML5 Canvas实现360度全景图的示例代码
2018/01/29 HTML / CSS
html5 外链式实现加减乘除的代码
2019/09/04 HTML / CSS
金讯Java笔试题目
2013/06/18 面试题
幼儿园中秋节活动方案
2014/02/06 职场文书
金融管理毕业生求职信
2014/03/03 职场文书
学生个人自我鉴定范文
2014/03/28 职场文书
2014年财务个人工作总结
2014/12/08 职场文书
2015年全国爱眼日活动小结
2015/02/27 职场文书
golang中字符串MD5生成方式总结
2021/07/04 Golang