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手册及PHP编程标准
Dec 17 PHP
用PHP实现的生成静态HTML速度快类库
Mar 31 PHP
PHP 采集程序中常用的函数
Dec 09 PHP
判断是否为指定长度内字符串的php函数
Feb 16 PHP
php中json_decode()和json_encode()的使用方法
Jun 04 PHP
php使用curl访问https示例分享
Jan 17 PHP
php smarty truncate UTF8乱码问题解决办法
Jun 13 PHP
php字符串替换函数substr_replace()用法实例
Mar 17 PHP
分享PHP函数实现数字与文字分页代码
Jul 28 PHP
Yii实现简单分页的方法
Apr 29 PHP
PHP操作Redis常用技巧总结
Apr 24 PHP
搭建PhpStorm+PhpStudy开发环境的超详细教程
Sep 17 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实现的漂亮分页方法
2014/04/17 PHP
PHP中的reflection反射机制测试例子
2014/08/05 PHP
Yii实现多按钮保存与提交的方法
2014/12/03 PHP
php计算到指定日期还有多少天的方法
2015/04/14 PHP
Laravel中log无法写入问题的解决
2017/06/17 PHP
PHP微信PC二维码登陆的实现思路
2017/07/13 PHP
利用php获得flv视频长度的实例代码
2017/10/26 PHP
转一个日期输入控件,支持FF
2007/04/27 Javascript
firefo xml 读写实现js代码
2009/06/11 Javascript
JS禁止查看网页源代码的实现方法
2016/10/12 Javascript
Bootstrap fileinput组件封装及使用详解
2017/03/10 Javascript
node.js平台下的mysql数据库配置及连接
2017/03/31 Javascript
深入理解react-router@4.0 使用和源码解析
2017/05/23 Javascript
JavaScript字符串_动力节点Java学院整理
2017/06/27 Javascript
理解Koa2中的async&amp;await的用法
2018/02/05 Javascript
create-react-app修改为多页面支持的方法
2018/05/17 Javascript
JavaScript之scrollTop、scrollHeight、offsetTop、offsetHeight等属性学习笔记
2020/07/15 Javascript
antd form表单数据回显操作
2020/11/02 Javascript
javascript实现京东快递单号的查询效果
2020/11/30 Javascript
原生js实现自定义滚动条组件
2021/01/20 Javascript
使用cx_freeze把python打包exe示例
2014/01/24 Python
Numpy数组转置的两种实现方法
2018/04/17 Python
python中的for循环
2018/09/28 Python
python 递归深度优先搜索与广度优先搜索算法模拟实现
2018/10/22 Python
Python3.5面向对象编程图文与实例详解
2019/04/24 Python
python视频按帧截取图片工具
2019/07/23 Python
pycharm修改file type方式
2019/11/19 Python
Python3之外部文件调用Django程序操作model等文件实现方式
2020/04/07 Python
django有哪些好处和优点
2020/09/01 Python
迪卡侬印尼体育用品商店:Decathlon印尼
2020/03/11 全球购物
文科教师毕业的自我评价
2014/01/16 职场文书
二年级数学教学反思
2014/01/21 职场文书
公司开业主持词
2015/07/02 职场文书
Python中json.dumps()函数的使用解析
2021/05/17 Python
React实现动效弹窗组件
2021/06/21 Javascript
SpringBoot中HttpSessionListener的简单使用方式
2022/03/17 Java/Android