PHP generates random cryptographic class sharing _php instance

Source: Internet
Author: User

Class Code:

<?php/** * Php-password Generator Class * Version 1.0.0 * */if (@!is_object ($passGen) | |!isset ($passGen)) {
$passGen = new Password;
 
  Class Password {/** * Capital letter A-Z * * @var array */protected $uppercase _chars;
 
  /** * lowercase letter A-Z * * @var array */protected $lowercase _chars;
 
  /** * Arabic numeral 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 a password * * @var array */protected $chars = Array ();
 
  /** * Password Length * * @var array/public $length;
 
  /** * Use capital Letter * * @var boolean/public $uppercase;
 
  /** * Use lowercase letter * * @var boolean/public $lowercase;
 
  /** * The use of Arabic numerals * * @var boolean/public $number;
 
  /** * Use special characters * * @var boolean/public $special; /** * Whether to useExtra special Characters * * @var boolean/public $extra;
     
    /** * Initialization Password setting * * @param int $length/function Password ($length = a) {$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_ch
    ARS); } if ($this->lowercase = $lowercase) = = True) {$this->chars = Array_merge ($this->chars, $this->lo
    Wer_chars); } if ($this->number = $number) = = True) {$this->chars = Array_merge ($this->chars, $this->number_c
    HARs); } if ($this->special = $special) = = True) {$this->chars = Array_merge ($this->chars, $this->specia
    L_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;
 
    /** * Generates a random number * * @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 to ABS (Intval ($value)); }
}

Call:

<?php
 
include_once ' password.class.php ';
 
echo $passGen->generate ();
 
Fs4yq74e2lee

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.