PHP Generates Random Cryptographic Class Sharing
- 2021-07-06 10:19:09
- OfStack
Class code:
<?php
/**
* PHP - Password Generator Class
* Version 1.0.0
*
*/
if (@!is_object($passGen) || !isset($passGen)) {
$passGen = new Password;
}
class Password
{
/**
* Capital letters A-Z
*
* @var array
*/
protected $uppercase_chars;
/**
* Lowercase letter a-z
*
* @var array
*/
protected $lowercase_chars;
/**
* Arabic numerals 0-9
*
* @var array
*/
protected $number_chars;
/**
* Special character
*
* @var array
*/
protected $special_chars;
/**
* Other special characters
*
* @var array
*/
protected $extra_chars;
/**
* All characters that are ultimately used to generate the password
*
* @var array
*/
protected $chars = array();
/**
* Password length
*
* @var array
*/
public $length;
/**
* Whether to use capital letters
*
* @var boolean
*/
public $uppercase;
/**
* Whether to use lowercase letters
*
* @var boolean
*/
public $lowercase;
/**
* Do you use Arabic numerals
*
* @var boolean
*/
public $number;
/**
* Whether to use special characters
*
* @var boolean
*/
public $special;
/**
* Whether to use extra special characters
*
* @var boolean
*/
public $extra;
/**
* Initialize password settings
*
* @param int $length
*/
function Password($length = 12)
{
$this->length = $length;
$this->configure(true, true, true, false, false);
}
/**
* Configure
*/
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);
}
/**
* Generate random passwords from character columns
*
* @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;
}
/**
* Generate random numbers
*
* @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));
}
}
Call:
<?php
include_once 'password.class.php';
echo $passGen->generate();
//FS4yq74e2LeE