@ 2014-11-03T21:55:59Z <?php
class Password
{
/**
* @var integer desired length of password, default: null
*/
protected $length;
/**
* @var array $numbers available numbers (0-9), default: []
*/
protected $numbers = [];
/**
* @var array $uppercase available uppercase characters (A-Z), default: []
*/
protected $uppercase = [];
/**
* @var array available lowercase characters (a-z), default: []
*/
protected $lowercase = [];
/**
* @var array available symbols (! @ # $ % ^ * - _ + = ?), default: []
*/
protected $symbols = [];
/**
* @var array characters to exclude from available characters - supersedes include, default: []
*/
protected $exclude = [];
/**
* @var array characters to include from available characters - overridden by exclude, default: []
*/
protected $include = [];
/**
* @var int amount of times to repeat the specified characters, default: 1
*/
protected $repeat = 1;
/**
* @var array list of characters to exclude when symbols is selected by default
*/
protected $extended = ['/', '\\', '(', ')', '<', '>', '\'', '"', '[', ']', '{', '}'];
/**
* @var array default values
*/
protected $options = [
'numbers' => true,
'uppercase' => true,
'lowercase' => false,
'symbols' => false,
'extended' => false,
'exclude' => [],
'include' => [],
'repeat' => 1
];
/**
* @var bool generate as html, default: false
*/
protected $html = false;
/**
* generates a password of the specified length using desired options
* attempts to prevent repeat characters unless repeat is greater than 1 or the specified length is greater than available characters
* automatically adjusts repeat based on desired length and desired characters to ensure desired length is reached
* @param integer $length desired length of password
* @param bool $numbers include numbers (0-9) in available characters, default: true
* @param bool $uppercase include uppercase characters (A-Z) in available characters, default: true
* @param bool $lowercase include lowercase characters (a-z) in available characters, default: false
* @param bool $symbols include symbols (! @ # $ % ^ * - _ + = ?) in available characters, default: false
* @param bool $extended include obscure symbols (, ), <, >, /, \, ", ', in available characters default: false
* @param array $exclude characters to exclude from available characters - supersedes include, default: array()
* @param array $include characters to include from available characters - overridden by exclude, default: array()
* @param integer $repeat how many times to repeat the specified characters, default: 1
* @return \ISEL\Auth\Password
*/
public function __construct($length, $numbers = null, $uppercase = null, $lowercase = null, $symbols = null, $extended = null, array $exclude = null, array $include = null, $repeat = null)
{
$this->setLength($length);
$this->setOptions([
'numbers' => $numbers,
'uppercase' => $uppercase,
'lowercase' => $lowercase,
'symbols' => $symbols,
'extended' => $extended,
'exclude' => $exclude,
'include' => $include,
'repeat' => $repeat
]);
}
/**
* generates a password of the specified length using desired options
* attempts to prevent repeat characters unless repeat is greater than 1 or the specified length is greater than available characters
* automatically adjusts repeat based on desired length and desired characters to ensure desired length is reached
* @param integer $length desired length of password
* @param bool $numbers include numbers (0-9) in available characters, default: true
* @param bool $uppercase include uppercase characters (A-Z) in available characters, default: true
* @param bool $lowercase include lowercase characters (a-z) in available characters, default: false
* @param bool $symbols include symbols (! @ # $ % ^ * - _ + = ?) in available characters, default: false
* @param bool $extended include obscure symbols (, ), <, >, /, \, ", ', in available characters default: false
* @param array $exclude characters to exclude from available characters - supersedes include, default: array()
* @param array $include characters to include from available characters - overridden by exclude, default: array()
* @param integer $repeat how many times to repeat the specified characters, default: 1
* @return string
*/
public static function factory($length, $numbers = null, $uppercase = null, $lowercase = null, $symbols = null, $extended = null, array $exclude = null, array $include = null, $repeat = null)
{
return new self($length, $numbers, $uppercase, $lowercase, $symbols, $extended, $exclude, $include, $repeat);
}
/**
* @param array $options set options using an array of values, and use defaults
*/
public function setOptions(array $options)
{
foreach ($this->options as $option => $default) {
$method = 'set' . ucfirst($option);
$value = (true === isset($options[$option]) && null !== $options[$option] ? $options[$option] : $default);
$this->$method($value);
}
}
/**
* convert password string to html friendly string
* @param $password
* @return string
*/
public static function passwordToHtml($password)
{
return htmlentities($password, ENT_HTML5 | ENT_QUOTES, false);
}
/**
* convert html entity string to literal string
* @param $password
* @return string
*/
public static function passwordFromHtml($password)
{
return html_entity_decode($password, ENT_HTML5 | ENT_QUOTES);
}
/**
* convert password string to html friendly string
* @param null $password
* @return string
*/
public function toHtml($password = null)
{
return self::passwordToHtml($password ? : $this->generate());
}
/**
* takes an html entity string and converts it to literal characters
* @param $password
* @return string
*/
public function fromHtml($password)
{
return self::passwordFromHtml($password);
}
/**
* output the password based on specified settings
* automatically adjusts based on supplied characters and desired length
* @return string
*/
public function output()
{
$characters = $this->getAvailableCharacters();
$string = substr(str_shuffle(str_repeat($characters, (ceil($this->length / strlen($characters)) * $this->repeat))), 0, $this->length);
if (true === empty($string)) {
throw new \RuntimeException('No string generated, check excluded characters.');
}
if (true === $this->html) {
$string = self::passwordToHtml($string);
}
return $string;
}
/**
* @return string
*/
public function getAvailableCharacters()
{
return implode('', array_diff(array_merge($this->include, $this->lowercase, $this->uppercase, $this->numbers, $this->symbols), array_merge($this->exclude, $this->extended)));
}
/**
* set Exclude
* @param array $exclude
* @return $this
*/
public function setExclude(array $exclude)
{
$this->exclude = $exclude;
return $this;
}
/**
* set Html
* @param boolean $html
* @return $this
*/
public function setHtml($html)
{
$this->html = $html;
return $this;
}
/**
* set Include
* @param array $include
* @return $this
*/
public function setInclude(array $include)
{
$this->include = $include;
return $this;
}
/**
* set Length
* @param int $length
* @return $this
*/
public function setLength($length)
{
$this->length = (int) $length;
return $this;
}
/**
* set Lowercase
* @param boolean $lowercase
* @return $this
*/
public function setLowercase($lowercase, $start = 'a', $end = 'z')
{
$this->lowercase = (true === (bool) $lowercase ? range(strtolower($start), strtolower($end)) : []);
return $this;
}
/**
* set Numbers
* @param boolean $numbers
* @return $this
*/
public function setNumbers($numbers, $start = 0, $end = 9)
{
$this->numbers = (true === (bool) $numbers ? range((int) $start, (int) $end) : []);
return $this;
}
/**
* set Repeat
* @param int $repeat
* @return $this
*/
public function setRepeat($repeat)
{
$this->repeat = (int) $repeat;
return $this;
}
/**
* set Symbols
* @param boolean $symbols include symbols
* @return $this
*/
public function setSymbols($symbols)
{
$this->symbols = (true === (bool) $symbols ? array_merge(range('!', '/'), range(':', '@'), range('[', '`')) : []);
return $this;
}
/**
* set Extended
* @param bool $extended include extended symbols, default: false
* @return $this
*/
public function setExtended($extended)
{
$this->extended = (false === (bool) $extended ? ['/', '\\', '(', ')', '<', '>', '\'', '"', '[', ']', '{', '}'] : []);
return $this;
}
/**
* set Uppercase
* @param boolean $uppercase
* @param string $start starting character, default: A
* @param string $end ending character, default: Z
* @return $this
*/
public function setUppercase($uppercase, $start = 'A', $end = 'Z')
{
$this->uppercase = (true === (bool) $uppercase ? range(strtoupper($start), strtoupper($end)) : []);
return $this;
}
/**
* @return string
*/
public function __toString()
{
return $this->output();
}
}
$temppasword = Password::factory(4096)->output();
echo $temppasword;
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.3.0 0.009 0.003 16.38 7.2.13 0.011 0.011 17.08 7.2.12 0.006 0.006 16.46 7.2.11 0.012 0.006 16.68 7.2.10 0.006 0.010 16.95 7.2.9 0.008 0.006 16.95 7.2.8 0.007 0.010 16.91 7.2.7 0.009 0.006 16.95 7.2.6 0.009 0.006 16.88 7.2.5 0.011 0.007 16.77 7.2.4 0.010 0.007 16.80 7.2.3 0.013 0.003 16.96 7.2.2 0.007 0.010 16.86 7.2.1 0.007 0.007 16.83 7.2.0 0.008 0.006 17.05 7.1.25 0.007 0.007 15.66 7.1.24 0.011 0.006 15.52 7.1.23 0.009 0.006 15.28 7.1.22 0.003 0.013 15.74 7.1.21 0.004 0.008 15.89 7.1.20 0.010 0.007 15.81 7.1.19 0.007 0.007 15.69 7.1.18 0.007 0.007 15.74 7.1.17 0.003 0.010 15.89 7.1.16 0.013 0.003 15.73 7.1.15 0.009 0.006 15.68 7.1.14 0.011 0.004 15.94 7.1.13 0.006 0.009 15.84 7.1.12 0.011 0.006 15.48 7.1.11 0.009 0.003 16.01 7.1.10 0.006 0.006 15.85 7.1.9 0.015 0.003 15.74 7.1.8 0.007 0.010 15.66 7.1.7 0.007 0.004 16.52 7.1.6 0.003 0.010 17.59 7.1.5 0.013 0.007 16.52 7.1.4 0.007 0.007 15.89 7.1.3 0.009 0.000 15.42 7.1.2 0.012 0.003 15.49 7.1.1 0.000 0.012 15.62 7.1.0 0.003 0.010 15.90 7.0.33 0.010 0.003 15.02 7.0.32 0.010 0.003 15.26 7.0.31 0.007 0.010 15.38 7.0.30 0.003 0.010 15.50 7.0.29 0.006 0.003 15.44 7.0.28 0.003 0.012 15.25 7.0.27 0.009 0.004 15.53 7.0.26 0.007 0.007 15.45 7.0.25 0.000 0.015 15.53 7.0.24 0.008 0.008 15.44 7.0.23 0.007 0.007 15.12 7.0.22 0.010 0.007 15.45 7.0.21 0.000 0.015 15.36 7.0.20 0.007 0.005 16.14 7.0.19 0.007 0.010 15.37 7.0.18 0.000 0.012 15.28 7.0.17 0.006 0.006 15.41 7.0.16 0.006 0.006 15.21 7.0.15 0.000 0.011 15.07 7.0.14 0.006 0.006 15.40 7.0.13 0.012 0.003 15.46 7.0.12 0.003 0.042 18.52 7.0.11 0.005 0.045 18.63 7.0.10 0.005 0.038 18.71 7.0.9 0.002 0.046 18.64 7.0.8 0.007 0.040 18.82 7.0.7 0.009 0.038 18.61 7.0.6 0.003 0.042 18.73 7.0.5 0.002 0.041 18.61 7.0.4 0.005 0.040 17.54 7.0.3 0.007 0.034 17.80 7.0.2 0.003 0.041 17.74 7.0.1 0.005 0.040 17.75 7.0.0 0.005 0.040 17.69 5.6.38 0.007 0.007 14.48 5.6.37 0.006 0.006 14.53 5.6.36 0.004 0.011 14.63 5.6.35 0.009 0.006 14.63 5.6.34 0.011 0.008 14.62 5.6.33 0.003 0.013 14.27 5.6.32 0.006 0.006 14.45 5.6.31 0.000 0.014 14.54 5.6.30 0.006 0.009 14.24 5.6.29 0.007 0.010 14.05 5.6.28 0.009 0.006 14.48 5.6.27 0.007 0.007 14.72 5.6.26 0.004 0.040 17.70 5.6.25 0.002 0.022 17.81 5.6.24 0.005 0.037 17.70 5.6.23 0.007 0.041 17.63 5.6.22 0.007 0.040 17.64 5.6.21 0.007 0.038 17.60 5.6.20 0.003 0.045 17.87 5.6.19 0.005 0.042 17.58 5.6.18 0.012 0.035 17.74 5.6.17 0.011 0.036 17.63 5.6.16 0.007 0.038 17.79 5.6.15 0.005 0.040 17.76 5.6.14 0.005 0.043 17.63 5.6.13 0.005 0.033 17.64 5.6.12 0.009 0.033 17.70 5.6.11 0.002 0.040 17.59 5.6.10 0.004 0.042 17.47 5.6.9 0.003 0.044 17.67 5.6.8 0.008 0.037 17.30 5.6.7 0.003 0.040 17.28 5.6.6 0.008 0.040 17.31 5.6.5 0.006 0.038 17.46 5.6.4 0.005 0.037 17.51 5.6.3 0.005 0.036 17.17 5.6.2 0.007 0.036 17.22 5.6.1 0.002 0.042 17.33 5.6.0 0.003 0.042 17.37 5.5.38 0.003 0.030 14.43 5.5.37 0.008 0.035 14.41 5.5.36 0.002 0.042 14.56 5.5.35 0.005 0.037 14.49 5.5.34 0.006 0.036 14.58 5.5.33 0.003 0.040 14.69 5.5.32 0.003 0.041 14.65 5.5.31 0.007 0.038 14.57 5.5.30 0.006 0.036 14.58 5.5.29 0.005 0.036 14.71 5.5.28 0.003 0.039 14.76 5.5.27 0.003 0.033 14.65 5.5.26 0.005 0.040 14.71 5.5.25 0.003 0.038 14.57 5.5.24 0.002 0.040 14.43 5.5.23 0.000 0.043 14.27 5.5.22 0.002 0.042 14.29 5.5.21 0.003 0.038 14.33 5.5.20 0.006 0.034 14.26 5.5.19 0.002 0.037 14.34 5.5.18 0.005 0.037 14.32 5.5.17 0.010 0.003 11.24 5.5.16 0.003 0.040 14.40 5.5.15 0.003 0.040 14.32 5.5.14 0.007 0.033 14.25 5.5.13 0.003 0.038 14.39 5.5.12 0.004 0.039 14.34 5.5.11 0.007 0.037 14.30 5.5.10 0.005 0.040 14.35 5.5.9 0.007 0.037 14.21 5.5.8 0.004 0.036 14.27 5.5.7 0.003 0.041 14.26 5.5.6 0.004 0.032 14.27 5.5.5 0.006 0.036 14.22 5.5.4 0.003 0.038 14.19 5.5.3 0.005 0.036 14.22 5.5.2 0.002 0.035 14.33 5.5.1 0.004 0.035 14.20 5.5.0 0.002 0.040 14.31 5.4.45 0.002 0.040 15.49 5.4.44 0.008 0.032 15.34 5.4.43 0.006 0.032 15.24 5.4.42 0.003 0.040 15.24 5.4.41 0.005 0.038 15.23 5.4.40 0.005 0.040 15.21 5.4.39 0.003 0.043 15.16 5.4.38 0.008 0.035 15.16 5.4.37 0.006 0.036 15.08 5.4.36 0.005 0.038 15.21 5.4.35 0.003 0.036 15.20 5.4.34 0.003 0.042 15.20 5.4.33 0.006 0.006 11.18 5.4.32 0.005 0.039 15.21 5.4.31 0.005 0.032 15.24 5.4.30 0.005 0.038 15.17 5.4.29 0.003 0.035 15.16 5.4.28 0.005 0.039 15.22 5.4.27 0.009 0.031 15.17 5.4.26 0.005 0.037 15.21 5.4.25 0.002 0.040 15.09 5.4.24 0.005 0.037 15.13 5.4.23 0.005 0.035 15.17 5.4.22 0.007 0.035 15.09 5.4.21 0.005 0.037 15.20 5.4.20 0.002 0.038 15.13 5.4.19 0.005 0.030 15.10 5.4.18 0.003 0.038 15.25 5.4.17 0.002 0.042 15.32 5.4.16 0.007 0.033 15.21 5.4.15 0.009 0.033 15.17 5.4.14 0.003 0.036 14.02 5.4.13 0.007 0.040 13.81 5.4.12 0.002 0.037 13.88 5.4.11 0.005 0.035 13.85 5.4.10 0.002 0.035 13.79 5.4.9 0.003 0.037 13.94 5.4.8 0.004 0.033 13.82 5.4.7 0.000 0.040 13.81 5.4.6 0.003 0.039 13.88 5.4.5 0.006 0.033 13.85 5.4.4 0.004 0.037 13.81 5.4.3 0.009 0.032 13.89 5.4.2 0.005 0.033 13.88 5.4.1 0.003 0.038 13.88 5.4.0 0.005 0.034 13.53
preferences:dark mode live preview
29.28 ms | 401 KiB | 5 Q