PHP生成隨機(jī)密碼類分享
更新時(shí)間:2014年06月25日 09:12:54 投稿:junjie
這篇文章主要介紹了PHP生成隨機(jī)密碼類分享,生成的密碼包含大小寫英文字母及數(shù)字,需要的朋友可以參考下
類代碼:
<?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;
/**
* 阿拉伯?dāng)?shù)字 0-9
*
* @var array
*/
protected $number_chars;
/**
* 特殊字符
*
* @var array
*/
protected $special_chars;
/**
* 其他特殊字符
*
* @var array
*/
protected $extra_chars;
/**
* 最終用來(lái)生成密碼的所有字符
*
* @var array
*/
protected $chars = array();
/**
* 密碼長(zhǎng)度
*
* @var array
*/
public $length;
/**
* 是否使用大寫字母
*
* @var boolean
*/
public $uppercase;
/**
* 是否使用小寫字母
*
* @var boolean
*/
public $lowercase;
/**
* 是否使用阿拉伯?dāng)?shù)字
*
* @var boolean
*/
public $number;
/**
* 是否使用特殊字符
*
* @var boolean
*/
public $special;
/**
* 是否使用額外的特殊字符
*
* @var boolean
*/
public $extra;
/**
* 初始化密碼設(shè)置
*
* @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);
}
/**
* 從字符列中生成隨機(jī)密碼
*
* @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;
}
/**
* 生成隨機(jī)數(shù)字
*
* @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));
}
}
調(diào)用:
<?php include_once 'password.class.php'; echo $passGen->generate(); //FS4yq74e2LeE
相關(guān)文章
linux下實(shí)現(xiàn)定時(shí)執(zhí)行php腳本
這篇文章主要介紹了linux下實(shí)現(xiàn)定時(shí)執(zhí)行php腳本的方法及具體使用示例,非常不錯(cuò)的文章,這里推薦給大家。2015-02-02
php實(shí)現(xiàn)信用卡校驗(yàn)位算法THE LUHN MOD-10示例
這篇文章主要介紹了php實(shí)現(xiàn)信用卡校驗(yàn)位算法THE LUHN MOD-10的示例,需要的朋友可以參考下2014-05-05
PHP的Laravel框架中使用消息隊(duì)列queue及異步隊(duì)列的方法
這篇文章主要介紹了PHP的Laravel框架中使用消息隊(duì)列queue及異步隊(duì)列的方法,針對(duì)Laravel 5.0后的版本,示例環(huán)境為L(zhǎng)inux系統(tǒng),需要的朋友可以參考下2016-03-03
php calender(日歷)二個(gè)版本代碼示例(解決2038問(wèn)題)
一個(gè)簡(jiǎn)單的php Calender(日歷),解決了2038問(wèn)題,這樣在32位機(jī)和64位機(jī)上都可以用了,代碼很簡(jiǎn)單,方便修改2013-12-12
新浪微博OAuth認(rèn)證和儲(chǔ)存的主要過(guò)程詳解
本文給大家介紹的是參考Twitter的認(rèn)證過(guò)程實(shí)現(xiàn)的新浪微博OAuth認(rèn)證和儲(chǔ)存的主要過(guò)程詳解2015-03-03

