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 和 MYSQL
Oct 09 PHP
解析PHP 使用curl提交json格式数据
Jun 29 PHP
微信扫描二维码登录网站代码示例
Dec 30 PHP
PHP转盘抽奖接口实例
Feb 09 PHP
PHP访问Google Search API的方法
Mar 05 PHP
thinkPHP模型初始化实例分析
Dec 03 PHP
PHP目录操作实例总结
Sep 27 PHP
PHP微信分享开发详解
Jan 14 PHP
PHP实现限制IP访问及提交次数的方法详解
Jul 17 PHP
Yii2框架实现登录、退出及自动登录功能的方法详解
Oct 24 PHP
浅谈PHP中如何实现Hook机制
Nov 14 PHP
laravel-admin表单提交隐藏一些数据,回调时获取数据的方法
Oct 08 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 Static关键字实用方法
2010/06/04 PHP
PHP生成RSS文件类实例
2014/12/05 PHP
php通过Chianz.com获取IP地址与地区的方法
2015/01/14 PHP
PHP实现生成唯一会员卡号
2015/08/24 PHP
php常用字符函数实例小结
2016/12/29 PHP
Yii框架实现的验证码、登录及退出功能示例
2017/05/20 PHP
PHP常见字符串操作函数与用法总结
2019/03/04 PHP
Yii框架函数简单用法分析
2019/09/09 PHP
基于Jquery的动态添加控件并取值的实现代码
2010/09/24 Javascript
js 获取计算后的样式写法及注意事项
2013/02/25 Javascript
JS特权方法定义作用以及与公有方法的区别
2013/03/18 Javascript
JavaScript中反正弦函数Math.asin()的使用简介
2015/06/14 Javascript
js代码实现点击按钮出现60秒倒计时
2021/01/28 Javascript
深入浅析JavaScript面向对象和原型函数
2016/02/06 Javascript
JavaScript性能优化之函数节流(throttle)与函数去抖(debounce)
2016/08/11 Javascript
基于MVC+EasyUI的web开发框架之使用云打印控件C-Lodop打印页面或套打报关运单信息
2016/08/29 Javascript
Angularjs中使用layDate日期控件示例
2017/01/11 Javascript
关于Sequelize连接查询时inlude中model和association的区别详解
2017/02/27 Javascript
vue2.0实现导航菜单切换效果
2017/05/08 Javascript
js处理包含中文的字符串实例
2017/10/11 Javascript
在angular 6中使用 less 的实例代码
2018/05/13 Javascript
js设置鼠标悬停改变背景色实现详解
2019/06/26 Javascript
vue点击按钮动态创建与删除组件功能
2019/12/29 Javascript
javascript实现点击产生随机图形
2021/01/25 Javascript
pygame加载中文名mp3文件出现error
2017/03/31 Python
python通过zabbix api获取主机
2018/09/17 Python
详解Python3 对象组合zip()和回退方式*zip
2019/05/15 Python
Python字符串函数strip()原理及用法详解
2020/07/23 Python
Python实现冒泡排序算法的完整实例
2020/11/04 Python
使用before和:after伪类制作css3圆形按钮
2014/04/08 HTML / CSS
精彩的大学生自我评价
2013/11/17 职场文书
入团者的自我评价分享
2013/12/02 职场文书
刑事辩护授权委托书
2014/09/13 职场文书
婚礼答谢词范文
2015/09/29 职场文书
小学秋季运动会加油口号及加油稿
2019/08/19 职场文书
Python实现对齐打印 format函数的用法
2022/04/28 Python