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 相关文章推荐
第六节--访问属性和方法
Nov 16 PHP
PHP批量生成缩略图的代码
Jul 19 PHP
Drupal7连接多个数据库及常见问题解决
Mar 02 PHP
php 字符串中的\n换行符无效、不能换行的解决方法
Apr 02 PHP
Destoon模板制作简明教程
Jun 20 PHP
ThinkPHP上使用多说评论插件的方法
Oct 31 PHP
php实现按指定大小等比缩放生成上传图片缩略图的方法
Dec 15 PHP
php面向对象中static静态属性和静态方法的调用
Feb 08 PHP
mysql_connect localhost和127.0.0.1的区别(网络层阐述)
Mar 26 PHP
php实现微信扫码自动登陆与注册功能
Sep 22 PHP
PHP实现微信图片上传到服务器的方法示例
Jun 29 PHP
YII2框架中excel表格导出的方法详解
Jul 21 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报表之jpgraph柱状图实例代码
2011/08/22 PHP
php获取从html表单传递数组的方法
2015/03/20 PHP
yii2.0整合阿里云oss上传单个文件的示例
2017/09/19 PHP
让Firefox支持event对象实现代码
2009/11/07 Javascript
JS,Jquery获取select,dropdownlist,checkbox下拉列表框的值(示例代码)
2014/01/11 Javascript
jquery实现类似EasyUI的页面布局可改变左右的宽度
2020/09/12 Javascript
JavaScript中的ubound函数使用实例
2014/11/04 Javascript
基于jQuery实现select下拉选择可输入附源码下载
2016/02/03 Javascript
vue.js 初体验之Chrome 插件开发实录
2017/05/13 Javascript
深入讲解xhr(XMLHttpRequest)/jsonp请求之abort
2017/07/26 Javascript
element ui 对话框el-dialog关闭事件详解
2018/02/26 Javascript
解决vue中修改了数据但视图无法更新的情况
2018/08/27 Javascript
详解VUE单页应用骨架屏方案
2019/01/17 Javascript
如何在项目中使用log4.js的方法步骤
2019/07/16 Javascript
使用Python脚本将文字转换为图片的实例分享
2015/08/29 Python
Python机器学习之SVM支持向量机
2017/12/27 Python
python对象与json相互转换的方法
2019/05/07 Python
pygame库实现移动底座弹球小游戏
2020/04/14 Python
Python迷宫生成和迷宫破解算法实例
2019/12/24 Python
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
2020/11/24 Python
canvas实现漂亮的下雨效果的示例
2018/04/18 HTML / CSS
挪威太阳镜和眼镜网上商城:SmartBuyGlasses挪威
2016/08/20 全球购物
欧洲著名的珠宝和手表网上商城:uhrcenter
2017/04/10 全球购物
美国瑜伽品牌:Gaiam
2017/10/31 全球购物
请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值
2014/09/15 面试题
升职自荐信范文
2013/10/05 职场文书
元旦联欢会主持词
2014/03/26 职场文书
班长竞选演讲稿
2014/04/24 职场文书
在校实习生求职信
2014/06/18 职场文书
计生工作先进事迹
2014/08/15 职场文书
绿色环保家庭事迹材料
2014/08/31 职场文书
自愿离婚协议书范本
2015/01/26 职场文书
协议书范文
2015/01/27 职场文书
优秀大学生自荐信
2015/03/26 职场文书
MySQL 隔离数据列和前缀索引的使用总结
2021/05/14 MySQL
如何利用Python实现一个论文降重工具
2021/07/09 Python