-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasswordGenerator.php
221 lines (205 loc) · 5.51 KB
/
PasswordGenerator.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* List of valid password generation types
*/
const TYPES = [
'numbers',
'letters',
'no_symbols',
'some_symbols',
'all',
];
/**
* Range of ascii codes for numbers
*/
const NUMBERS = [48, 57];
/**
* Range of ascii codes for capital letters
*/
const CAPITAL_LETTERS = [65, 90];
/**
* Range of ascii codes for small letters
*/
const SMALL_LETTERS = [97, 122];
/**
* Range of ascii codes for all type
*/
const ALL = [33, 126];
/**
* The class 'PasswordGenerator' manages the random password generation.
*/
class PasswordGenerator
{
/**
* The password type to generate
* (if symbols should contained etc.)
*
* @var string
*/
private string $type = '';
/**
* Password length
*
* @var int
*/
private int $length = 0;
/**
* Constructs an instance of 'PasswordGenerator'.
*
* @param string $type password type
* @param int $length password length (default = 8)
*/
public function __construct(string $type, int $length = 8)
{
$this->setType($type);
$this->setLength($length);
}
/**
* Creates a password.
*
* @return string password
*/
public function createPassword() : string
{
if ($this->type === 'numbers') {
return $this->createPasswordNumbers();
}
if ($this->type === 'letters') {
return $this->createPasswordLetters();
}
if ($this->type === 'no_symbols') {
return $this->createPasswordNoSymbols();
}
if ($this->type === 'some_symbols') {
return $this->createPasswordSomeSymbols();
}
if ($this->type === 'all') {
return $this->createPasswordAll();
}
throw new RuntimeException('Cannot create password because of invalid type!');
}
/**
* Creates a password which only contains numbers.
*
* @return string password
*/
private function createPasswordNumbers() : string
{
$password = '';
for ($i = 0; $i < $this->length; $i++) {
$number = rand(NUMBERS[0], NUMBERS[1]);
$password .= chr($number);
}
return $password;
}
/**
* Creates a password which only contains letters.
*
* @return string password
*/
private function createPasswordLetters() : string
{
$password = '';
for ($i = 0; $i < $this->length; $i++) {
$number = rand(CAPITAL_LETTERS[0], CAPITAL_LETTERS[0] + 51);
if ($number > CAPITAL_LETTERS[1]) {
$number += (SMALL_LETTERS[0] - CAPITAL_LETTERS[1]);
}
$password .= chr($number);
}
return $password;
}
/**
* Creates a password which contains numbers and letters.
*
* @return string password
*/
private function createPasswordNoSymbols() : string
{
$password = '';
for ($i = 0; $i < $this->length; $i++) {
$number = rand(NUMBERS[0], NUMBERS[0] + 61);
if ($number > NUMBERS[1]) {
$number += (CAPITAL_LETTERS[0] - NUMBERS[1]);
}
if ($number > CAPITAL_LETTERS[1]) {
$number += (SMALL_LETTERS[0] - CAPITAL_LETTERS[1]);
}
$password .= chr($number);
}
return $password;
}
/**
* Creates a password which contains numbers, letters and usual symbols.
*
* @return string password
*/
private function createPasswordSomeSymbols() : string
{
$password = '';
for ($i = 0; $i < $this->length; $i++) {
$number = rand(NUMBERS[0], NUMBERS[0] + 61);
if ($number > NUMBERS[1]) {
$number += (CAPITAL_LETTERS[0] - NUMBERS[1]);
}
if ($number > CAPITAL_LETTERS[1]) {
$number += (SMALL_LETTERS[0] - CAPITAL_LETTERS[1]);
}
if ($number > SMALL_LETTERS[1]) {
if ($number === SMALL_LETTERS[1] + 1) {
$number = 95;
} else if ($number === SMALL_LETTERS[1] + 2) {
$number = 123;
} else if ($number === SMALL_LETTERS[1] + 3) {
$number = 125;
}
}
$password .= chr($number);
}
return $password;
}
/**
* Creates a password which contains numbers, letters and all symbols.
*
* @return string password
*/
private function createPasswordAll() : string
{
$password = '';
for ($i = 0; $i < $this->length; $i++) {
$number = rand(ALL[0], ALL[1]);
$password .= chr($number);
}
return $password;
}
/**
* Sets the password type.
*
* @param string $type type to set
* @return self
*/
private function setType(string $type) : self
{
foreach (TYPES as $validType) {
if ($type === $validType) {
$this->type = $type;
return $this;
}
}
throw new InvalidArgumentException('Invalid type!');
}
/**
* Sets the password length.
*
* @param int $length length to set
* @return self
*/
private function setLength(int $length) : self
{
if ($length < 1 || $length > 1000) {
throw new InvalidArgumentException('Length must be between 1 and 1000');
}
$this->length = $length;
return $this;
}
}