-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_generator.php
53 lines (46 loc) · 1.59 KB
/
password_generator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types = 1);
require_once "passgen_interface.php";
class PasswordGenerator implements PassGenInterface
{
private $letters_a;
private $letters_b;
private $numbers;
private $symbols;
public function __construct()
{
$this->letters_a = range('A', 'Z');
$this->letters_b = range('a', 'z');
$this->numbers = range('0', '9');
$this->symbols = ['.', ',', '!', '#', '$', '%', '&', '*', '(', ')', '+', '-', '/', ';', '~', '<', '>'];
}
public function generate(int $length, int $count = 1, bool $use_numbers = True, bool $use_symbols = True): void
{
if ($length < 1) $length = 1;
if ($count < 1) $count = 1;
$passwords_array = array();
$chars = array_merge($this->letters_a, $this->letters_b);
if ($use_numbers) {
$chars = array_merge($chars, $this->numbers, $this->numbers);
}
if ($use_symbols) {
$chars = array_merge($chars, $this->symbols, $this->symbols);
}
shuffle($chars);
foreach (range(0, $count - 1) as $x) {
$password = "";
foreach (range(0, $length - 1) as $index) {
$rand_key = array_rand($chars);
$password .= $chars[$rand_key];
}
$passwords_array[] = $password;
}
$this->print_passwords($passwords_array);
}
private function print_passwords(array $passwords): void
{
foreach ($passwords as $password) {
echo $password . PHP_EOL;
}
}
}