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 相关文章推荐
桌面中心(一)创建数据库
Oct 09 PHP
PHP新手上路(九)
Oct 09 PHP
php获得文件扩展名三法
Nov 25 PHP
php程序之die调试法 快速解决错误
Sep 17 PHP
php array_filter除去数组中的空字符元素
Jun 21 PHP
深入密码加salt原理的分析
Jun 06 PHP
php广告加载类用法实例
Sep 23 PHP
php+mysqli使用面向对象方式更新数据库实例
Jan 29 PHP
使用 PHPStorm 开发 Laravel
Mar 24 PHP
php中二维数组排序问题方法详解
Aug 28 PHP
PHP+JQUERY操作JSON实例
Mar 23 PHP
PHP经典实用正则表达式小结
May 04 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
实用函数8
2007/11/08 PHP
php AJAX实例根据邮编自动完成地址信息
2008/11/23 PHP
PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
2014/10/20 PHP
jquery.simple.tree插件 更简单,兼容性更好的无限树插件
2010/09/03 Javascript
基于Bootstrap的后台管理面板 Bootstrap Metro Dashboard
2016/06/17 Javascript
VUE实现日历组件功能
2017/03/13 Javascript
JS检测window.open打开的窗口是否关闭
2017/06/25 Javascript
js删除数组中的元素delete和splice的区别详解
2018/02/03 Javascript
微信小程序中使用ECharts 异步加载数据实现图表功能
2018/07/13 Javascript
vue异步axios获取的数据渲染到页面的方法
2018/08/09 Javascript
ng-repeat指令在迭代对象时的去重方法
2018/10/02 Javascript
使用js在layui中实现上传图片压缩
2019/06/18 Javascript
JavaScript中变量提升机制示例详解
2019/12/27 Javascript
Vue向后台传数组数据,springboot接收vue传的数组数据实例
2020/11/12 Javascript
python实现简单的socket server实例
2015/04/29 Python
python字符串的常用操作方法小结
2016/05/21 Python
python连接mysql实例分享
2016/10/09 Python
简单谈谈python中的多进程
2016/11/06 Python
Python机器学习算法之k均值聚类(k-means)
2018/02/23 Python
selenium+python 去除启动的黑色cmd窗口方法
2018/05/22 Python
Python 移动光标位置的方法
2019/01/20 Python
python中的单引号双引号区别知识点总结
2019/06/23 Python
centos 安装Python3 及对应的pip教程详解
2019/06/28 Python
PyQt5实现暗黑风格的计时器
2019/07/29 Python
Macbook安装Python最新版本、GUI开发环境、图像处理、视频处理环境详解
2020/02/17 Python
Python sorted排序方法如何实现
2020/03/31 Python
2021年的Python 时间轴和即将推出的功能详解
2020/07/27 Python
利用HTML5 Canvas API绘制矩形的超级攻略
2016/03/21 HTML / CSS
Geekbuying波兰:购买中国电子产品
2019/10/20 全球购物
梅西百货官网:Macy’s
2020/08/04 全球购物
大学生职业规划论文
2014/01/11 职场文书
岗位职责风险点
2014/03/12 职场文书
2014学习优秀共产党员先进事迹思想汇报
2014/09/14 职场文书
财务务虚会发言材料
2014/10/20 职场文书
酒店销售经理岗位职责
2015/04/02 职场文书
2016年小学推普宣传周活动总结
2016/04/06 职场文书