Skip to content

Instantly share code, notes, and snippets.

@vaneves
Last active November 20, 2019 00:23
Show Gist options
  • Save vaneves/3b600500378491020d3db8b30cf3d2ba to your computer and use it in GitHub Desktop.
Save vaneves/3b600500378491020d3db8b30cf3d2ba to your computer and use it in GitHub Desktop.
Password generator
<?php
$passworder = new Passworder(Passworder::LEVEL_GOD);
echo $passworder->new(64, 128);
<?php
class Passworder
{
const CHARS = [
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'!#$%&()*+,-./:;<=>?[\]^_`{|}~',
'àáãâäèéêëìíîïòóõôöúùûüñýÿÀÁÃÂÄÈÉÊËÌÍÎÏÒÓÕÔÖÚÙÛÜÑÝŸ',
'§“”‘’‹›«»…¦•¶♠♣♥♦©®™£¢€¥¤æš',
];
const MIN = 32;
const MAX = 64;
const LEVEL_BASIC = 0;
const LEVEL_SUPER = 1;
const LEVEL_POWER = 2;
const LEVEL_GOD = 3;
private $level;
private $chars = null;
public function __construct($level = null)
{
$this->level = $level;
if ($this->level === null) {
$this->level = self::LEVEL_BASIC;
}
$this->chars = self::CHARS[0];
if ($this->level >= self::LEVEL_SUPER) {
$this->chars .= self::CHARS[1];
}
if ($this->level >= self::LEVEL_POWER) {
$this->chars .= self::CHARS[2];
}
if ($this->level >= self::LEVEL_GOD) {
$this->chars .= self::CHARS[3];
}
}
private function char()
{
$char = mb_substr($this->chars, rand(0, mb_strlen($this->chars) - 1), 1, 'UTF-8');
return $char;
}
public function new($min = null, $max = null)
{
$min = !$min ? self::MIN : $min;
$max = !$max ? self::MAX : $max;
if ($max < $min) {
$max = $min;
}
$length = rand($min, $max);
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $this->char();
}
return $password;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment