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继承的一个应用
Sep 06 PHP
ajax返回值中有回车换行、空格的解决方法分享
Oct 24 PHP
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
Jun 12 PHP
php中异常处理方法小结
Jan 09 PHP
PHP、Java des加密解密实例
Apr 27 PHP
微信支付PHP SDK之微信公众号支付代码详解
Dec 09 PHP
php使用curl通过代理获取数据的实现方法
May 16 PHP
PHP单例模式简单用法示例
Jun 23 PHP
PHP通过文件路径获取文件名的实例代码
Oct 14 PHP
PHP实现数组和对象的相互转换操作示例
Mar 20 PHP
Laravel 5.5 实现禁用用户注册示例
Oct 24 PHP
PHP设计模式之组合模式定义与应用示例
Feb 01 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
通过百度地图获取公交线路的站点坐标的js代码
2012/05/11 Javascript
通过JS自动隐藏手机浏览器的地址栏实现原理与代码
2013/01/02 Javascript
Javascript 鼠标移动上去小三角形滑块缓慢跟随效果
2013/04/26 Javascript
jquery 设置元素相对于另一个元素的top值(实例代码)
2013/11/06 Javascript
jQuery手机浏览器中拖拽动作的艰难性分析
2015/02/04 Javascript
Javascript实现div的toggle效果实例分析
2015/06/09 Javascript
jQuery三级下拉列表导航菜单代码分享
2020/04/15 Javascript
jquery制作属于自己的select自定义样式
2015/11/23 Javascript
理解JavaScript事件对象
2016/01/25 Javascript
EasyUI闪屏EasyUI页面加载提示(原理+代码+效果图)
2016/02/21 Javascript
jQuery选择器及jquery案例详解(必看)
2016/05/20 Javascript
js只执行1次的函数示例
2016/07/20 Javascript
jQuery中layer分页器的使用
2017/03/13 Javascript
JS判断Android、iOS或浏览器的多种方法(四种方法)
2017/06/29 Javascript
ajax+node+request爬取网络图片的实例(宅男福利)
2017/08/28 Javascript
使用Vue完成一个简单的todolist的方法
2017/12/01 Javascript
Javascript操作select控件代码实例
2020/02/14 Javascript
[07:55]2014DOTA2 TI正赛第三日 VG上演推进荣耀DKEG告别
2014/07/21 DOTA
Python判断文件和字符串编码类型的实例
2017/12/21 Python
python 高效去重复 支持GB级别大文件的示例代码
2018/11/08 Python
详解Python装饰器
2019/03/25 Python
python调试神器PySnooper的使用
2019/07/03 Python
Python threading.local代码实例及原理解析
2020/03/16 Python
Windows10+anacond+GPU+pytorch安装详细过程
2020/03/24 Python
OpenCV4.1.0+VS2017环境配置的方法步骤
2020/07/09 Python
html5标记文字_动力节点Java学院整理
2017/07/11 HTML / CSS
美国牙科折扣计划:DentalPlans.com
2019/08/26 全球购物
青年创业培训欢迎词
2014/01/08 职场文书
党的群众教育实践活动实施方案
2014/06/12 职场文书
质量提升方案
2014/06/16 职场文书
安全口号大全
2014/06/21 职场文书
2015年调度员工作总结
2015/04/30 职场文书
办公用品管理制度
2015/08/04 职场文书
自书遗嘱范文
2015/08/07 职场文书
小学思品教学反思
2016/02/20 职场文书
OpenCV-Python实现油画效果的实例
2021/06/08 Python