/
Password.php
297 lines (274 loc) · 9.95 KB
/
Password.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* Library for the password
*
* Features and advantages
* * Quality checking (complexity) password
* * Generation of high-quality and resistant to cracking password
* * A method that returns derivatives from the password to be able to log in (authorization),
* regardless of keyboard layout (language input) and pressing [Caps Lock] or [Shift] buttons.
* Security remark
* The method returns the only a few password forms for a successful log in (authorization) possibility.
* It's too little against the billions combinations of high-quality passwords.
* However, the usefulness of this feature may be condemned by some critics.
*
* Example (derivatives of password)
* en, [Caps Lock] off: abCD1%
* en, [Caps Lock] on: ABcd1%
* ru, [Caps Lock] off: фиСВ1%
* ru, [Caps Lock] on: ФИсв1%
*
* History
* This class has been used successfully in August 2009 in several commercial projects
*
* TODO
* http://ru.wikipedia.org/wiki/Раскладка_клавиатуры
*
* Useful links
* http://allanguages.info/
*
* @link http://code.google.com/p/php-password/
* @license http://creativecommons.org/licenses/by-sa/3.0/
* @author Nasibullin Rinat
* @version 1.1.0
*/
class Password
{
const ALNUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
#calling the methods of this class only statically!
private function __construct() {}
/**
* Quality checking (complexity) password
*
* Bad passwords
* * length < 6 or > 20 characters
* * characters beyond the range of \x20-\x7e
* (non-ASCII can not be used because this can cause problems with charsets)
* * does not contain both letters and numbers
* * sequence of characters as the keyboard (123456, qwerty)
* * halves, as on a keyboard (qazxsw), repeated (werwer, 12,341,234) and
* "reflected" (123 321, qweewq) sequences are included here
* * unique characters less than 46 % (wwwfff, 000000)
*
* За основу был взят оригинальный скрипт Tronyx (http://forum.dklab.ru/viewtopic.php?t=7014),
* который был модифицирован и улучшен.
*
* @param string $password Password
* @param bool $is_check_digits Check for the numbers exists?
* @param bool $is_check_letters Check for the latin letters exists?
* @return bool TRUE, if the password is good and FALSE,
* if the password is weak or error occurred
*/
public static function quality_check($password, $is_check_digits = true, $is_check_letters = true)
{
if (! ReflectionTypeHint::isValid()) return false;
#проверка минимальной длины и допустимых символов
if (! preg_match('/^[\x20-\x7e]{6,20}$/sSX', $password)) return false;
#проверка на цифры
if ($is_check_digits && ! preg_match('/\d/sSX', $password)) return false;
#проверка на латинские буквы
if ($is_check_letters && ! preg_match('/[a-zA-Z]/sSX', $password)) return false;
#последовательность символов как на клавиатуре (123456, qwerty, qazwsx, abcdef)
$chars = '`1234567890-=\\'. #второй ряд клавиш, [Shift] off
'~!@#$%^&*()_+|'. #второй ряд клавиш, [Shift] on
'qwertyuiop[]asdfghjkl;\'zxcvbnm,./'. #по горизонтали (расшир. диапазон)
'QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?'. #по горизонтали (расшир. диапазон)
'qwertyuiopasdfghjklzxcvbnm'. #по горизонтали
'QWERTYUIOPASDFGHJKLZXCVBNM'. #по горизонтали
'qazwsxedcrfvtgbyhnujmikolp'. #по диагонали
'QAZWSXEDCRFVTGBYHNUJMIKOLP'. #по диагонали
'abcdefghijklmnopqrstuvwxyz'. #по алфавиту
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; #по алфавиту
if (strpos($chars, $password) !== false) return false;
if (strpos($chars, strrev($password)) !== false) return false;
$length = strlen($password);
#половинки, как на клавиатуре (повторные и "отражённые" последовательности сюда включаются)
if ($length > 5 && $length % 2 == 0)
{
$c = $length / 2;
$left = substr($password, 0, $c); #первая половина пароля
$right = substr($password, $c); #вторая половина пароля
$is_left = (strpos($chars, $left) !== false or strpos($chars, strrev($left)) !== false);
$is_right = (strpos($chars, $right) !== false or strpos($chars, strrev($right)) !== false);
if ($is_left && $is_right) return false;
}
#процент уникальности символов
$k = strlen(count_chars($password, 3)) / $length;
if ($k < 0.46) return false;
return true;
}
/**
* Generates high-quality and resistant to cracking password
*
* @param int|digit $length Length of the string output
* @param string $chars Alphabet to create a pseudorandom string
* by default [2-9a-zA-NP-Z]
* (Except 0O1l, because these letters and numbers are difficult to discern visually)
* @return string|bool Returns FALSE if error occurred
*/
public static function generate($length = 8, $chars = '23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ')
{
if (! ReflectionTypeHint::isValid()) return false;
#36 ^ 6 = 2 176 782 336 unique combinations minimum
if ($length < 6)
{
trigger_error('Minimum length of password is 6 chars, ' . $length . ' given!', E_USER_WARNING);
return false;
}
$chars = count_chars($chars, $mode = 3); #gets unique chars
$len = strlen($chars);
if ($len < 36)
{
trigger_error('Minimum length of alphabet chars is 36 unique chars (e. g. [a-z\d] in regexp terms), ' . $len . ' given!', E_USER_WARNING);
return false;
}
mt_srand((double)microtime() * 1000000); #initialize
$c = 0;
do
{
for ($password = '', $i = 0; $i < $length; $i++) $password .= substr($chars, mt_rand(0, $len - 1), 1);
$c++;
if ($c > 100)
{
#protects for endless cycle
trigger_error('Endless cycle found!', E_USER_WARNING);
return false;
}
}
while (! self::quality_check($password));
return $password;
}
/**
* Returns derivatives from the password to be able to log in (authorization),
* regardless of keyboard layout (language input) and pressing [Caps Lock] or [Shift] buttins.
* Character encoding — UTF-8.
*
* @param string $password Password
* @param string $lang Language
* @return array|bool A few password forms
* Returns FALSE if error occurred
*/
public static function keyboard_forms($password, $lang = 'ru')
{
if (! ReflectionTypeHint::isValid()) return false;
$passwords = array(
//$password,
self::_keyboard_layout_conv($password, $lang, 'en'),
self::_keyboard_layout_conv($password, 'en', $lang),
);
for ($c = count($passwords), $i = 0; $i < $c; $i++)
$passwords[] = self::_keyboard_capslock_invert($passwords[$i]);
return $passwords;
}
/**
* Converts lowercase characters to uppercase and vice versa.
*
* @param string $s
* @return string
*/
private static function _keyboard_capslock_invert($s)
{
$trans = UTF8::$convert_case_table;
$trans += array_flip($trans);
return strtr($s, $trans);
}
/**
* Converts text from one keyboard to another.
* Character encoding - UTF-8.
*
* Globalize your On Demand Business: logical keyboard layout registry index
* Keyboard layouts for countries and regions around the world.
* http://www-306.ibm.com/software/globalization/topics/keyboards/registry_index.jsp
*
* @param string $s Text in UTF-8
* @param string $input Input keyboard layout (en, ru)
* @param string $output Output keyboard layout (en, ru)
* @return string|bool String on success, FALSE on error
*/
private static function _keyboard_layout_conv($s, $input, $output)
{
#QWERTY раскладка клавиатуры для русского и английского языка
static $trans_en_ru = array(
#[CapsLock] off
'`' => 'ё',
'q' => 'й',
'w' => 'ц',
'e' => 'у',
'r' => 'к',
't' => 'е',
'y' => 'н',
'u' => 'г',
'i' => 'ш',
'o' => 'щ',
'p' => 'з',
'[' => 'х',
']' => 'ъ',
'a' => 'ф',
's' => 'ы',
'd' => 'в',
'f' => 'а',
'g' => 'п',
'h' => 'р',
'j' => 'о',
'k' => 'л',
'l' => 'д',
';' => 'ж',
'\'' => 'э',
'z' => 'я',
'x' => 'ч',
'c' => 'с',
'v' => 'м',
'b' => 'и',
'n' => 'т',
'm' => 'ь',
',' => 'б',
'.' => 'ю',
'/' => '.',
#[CapsLock] on
'~' => 'Ё',
'@' => '"',
'#' => '№',
'$' => ';',
'^' => ':',
'&' => '?',
'|' => '/',
'Q' => 'Й',
'W' => 'Ц',
'E' => 'У',
'R' => 'К',
'T' => 'Е',
'Y' => 'Н',
'U' => 'Г',
'I' => 'Ш',
'O' => 'Щ',
'P' => 'З',
'{' => 'Х',
'}' => 'Ъ',
'A' => 'Ф',
'S' => 'Ы',
'D' => 'В',
'F' => 'А',
'G' => 'П',
'H' => 'Р',
'J' => 'О',
'K' => 'Л',
'L' => 'Д',
':' => 'Ж',
'"' => 'Э',
'Z' => 'Я',
'X' => 'Ч',
'C' => 'С',
'V' => 'М',
'B' => 'И',
'N' => 'Т',
'M' => 'Ь',
'<' => 'Б',
'>' => 'Ю',
'?' => ',',
);
if ($input === 'en' && $output === 'ru') return strtr($s, $trans_en_ru);
if ($input === 'ru' && $output === 'en') return strtr($s, array_flip($trans_en_ru));
trigger_error('Unsupported input and output keyboard layouts!', E_USER_WARNING);
return false;
}
}