|
1 | 1 | <?php |
2 | 2 | /* |
3 | | - Code modified from https://gitlab.com/garybell/password-validation/ (MIT licensed) |
| 3 | + Code modified from https://gitlab.com/garybell/password-validation/ (MIT licensed) |
4 | 4 | */ |
5 | 5 | namespace Pdsinterop\PhpSolid; |
6 | 6 |
|
7 | 7 | class PasswordValidator |
8 | 8 | { |
9 | | - private static string $specialCharacters = ' !"#$%&\'()*+,-./:;<=>?@[\]^_{|}~'; |
10 | | - private static string $lowercaseCharacters = 'abcdefghijklmnopqrstuvwxyz'; |
11 | | - private static string $uppercaseCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
12 | | - private static string $numbers = '0123456789'; |
| 9 | + private static string $specialCharacters = ' !"#$%&\'()*+,-./:;<=>?@[\]^_{|}~'; |
| 10 | + private static string $lowercaseCharacters = 'abcdefghijklmnopqrstuvwxyz'; |
| 11 | + private static string $uppercaseCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 12 | + private static string $numbers = '0123456789'; |
13 | 13 |
|
14 | | - /** |
15 | | - * The maximum number of times the same character can appear in the password |
16 | | - * @var int |
17 | | - */ |
18 | | - private static int $maxOccurrences = 2; |
| 14 | + /** |
| 15 | + * The maximum number of times the same character can appear in the password |
| 16 | + * @var int |
| 17 | + */ |
| 18 | + private static int $maxOccurrences = 2; |
19 | 19 |
|
20 | | - /** |
21 | | - * Get the base amount of characters from the characters used in the password. |
22 | | - * This is the number of possible characters to pick from in the used character sets |
23 | | - * i.e. 26 for only lower case passwords |
24 | | - * @param $password |
25 | | - * @return int |
26 | | - */ |
27 | | - public static function getBase(string $password): int |
28 | | - { |
29 | | - $characters = str_split($password); |
30 | | - $base = 0; |
31 | | - $hasSpecial = false; |
32 | | - $hasLower = false; |
33 | | - $hasUpper = false; |
34 | | - $hasDigits = false; |
| 20 | + /** |
| 21 | + * Get the base amount of characters from the characters used in the password. |
| 22 | + * This is the number of possible characters to pick from in the used character sets |
| 23 | + * i.e. 26 for only lower case passwords |
| 24 | + * @param $password |
| 25 | + * @return int |
| 26 | + */ |
| 27 | + public static function getBase(string $password): int |
| 28 | + { |
| 29 | + $characters = str_split($password); |
| 30 | + $base = 0; |
| 31 | + $hasSpecial = false; |
| 32 | + $hasLower = false; |
| 33 | + $hasUpper = false; |
| 34 | + $hasDigits = false; |
35 | 35 |
|
36 | | - foreach ($characters as $character) { |
37 | | - if (!$hasLower && strpos(self::$lowercaseCharacters, $character) !== false) { |
38 | | - $hasLower = true; |
39 | | - $base += strlen(self::$lowercaseCharacters); |
40 | | - } |
41 | | - if (!$hasUpper && strpos(self::$uppercaseCharacters, $character) !== false) { |
42 | | - $hasUpper = true; |
43 | | - $base += strlen(self::$uppercaseCharacters); |
44 | | - } |
45 | | - if (!$hasSpecial && strpos(self::$specialCharacters, $character) !== false) { |
46 | | - $hasSpecial = true; |
47 | | - $base += strlen(self::$specialCharacters); |
48 | | - } |
49 | | - if (!$hasDigits && strpos(self::$numbers, $character) !== false) { |
50 | | - $hasDigits = true; |
51 | | - $base += strlen(self::$numbers); |
52 | | - } |
| 36 | + foreach ($characters as $character) { |
| 37 | + if (!$hasLower && strpos(self::$lowercaseCharacters, $character) !== false) { |
| 38 | + $hasLower = true; |
| 39 | + $base += strlen(self::$lowercaseCharacters); |
| 40 | + } |
| 41 | + if (!$hasUpper && strpos(self::$uppercaseCharacters, $character) !== false) { |
| 42 | + $hasUpper = true; |
| 43 | + $base += strlen(self::$uppercaseCharacters); |
| 44 | + } |
| 45 | + if (!$hasSpecial && strpos(self::$specialCharacters, $character) !== false) { |
| 46 | + $hasSpecial = true; |
| 47 | + $base += strlen(self::$specialCharacters); |
| 48 | + } |
| 49 | + if (!$hasDigits && strpos(self::$numbers, $character) !== false) { |
| 50 | + $hasDigits = true; |
| 51 | + $base += strlen(self::$numbers); |
| 52 | + } |
53 | 53 |
|
54 | | - if ( |
55 | | - strpos(self::$lowercaseCharacters, $character) === false |
56 | | - && strpos(self::$uppercaseCharacters, $character) === false |
57 | | - && strpos(self::$specialCharacters, $character) === false |
58 | | - && strpos(self::$numbers, $character) === false |
59 | | - ) { |
60 | | - $base++; |
61 | | - } |
62 | | - } |
| 54 | + if ( |
| 55 | + strpos(self::$lowercaseCharacters, $character) === false |
| 56 | + && strpos(self::$uppercaseCharacters, $character) === false |
| 57 | + && strpos(self::$specialCharacters, $character) === false |
| 58 | + && strpos(self::$numbers, $character) === false |
| 59 | + ) { |
| 60 | + $base++; |
| 61 | + } |
| 62 | + } |
63 | 63 |
|
64 | | - return $base; |
65 | | - } |
| 64 | + return $base; |
| 65 | + } |
66 | 66 |
|
67 | | - /** |
68 | | - * get the calculated entropy of the password based on the rules for excluding duplicate characters |
69 | | - * If a password is in the banned list, entropy will be 0. |
70 | | - * @see bannedPassords() |
71 | | - * @param string $password |
72 | | - * @param array $bannedPasswords a custom list of passwords to disallow |
73 | | - * @return float |
74 | | - */ |
75 | | - public static function getEntropy(string $password, array $bannedPasswords = []): float |
76 | | - { |
77 | | - if (in_array(strtolower($password), $bannedPasswords)) { |
78 | | - // these are so weak, we just want to outright ban them. Entropy will be 0 for anything in this list. |
79 | | - return 0; |
80 | | - } |
81 | | - $base = self::getBase($password); |
82 | | - $length = self::getLength($password); |
| 67 | + /** |
| 68 | + * get the calculated entropy of the password based on the rules for excluding duplicate characters |
| 69 | + * If a password is in the banned list, entropy will be 0. |
| 70 | + * @see bannedPassords() |
| 71 | + * @param string $password |
| 72 | + * @param array $bannedPasswords a custom list of passwords to disallow |
| 73 | + * @return float |
| 74 | + */ |
| 75 | + public static function getEntropy(string $password, array $bannedPasswords = []): float |
| 76 | + { |
| 77 | + if (in_array(strtolower($password), $bannedPasswords)) { |
| 78 | + // these are so weak, we just want to outright ban them. Entropy will be 0 for anything in this list. |
| 79 | + return 0; |
| 80 | + } |
| 81 | + $base = self::getBase($password); |
| 82 | + $length = self::getLength($password); |
83 | 83 |
|
84 | | - $decimalPlaces = 2; |
85 | | - return number_format(log($base ** $length), $decimalPlaces); |
86 | | - } |
| 84 | + $decimalPlaces = 2; |
| 85 | + return number_format(log($base ** $length), $decimalPlaces); |
| 86 | + } |
87 | 87 |
|
88 | | - /** |
89 | | - * Check the length of the password based on known rules |
90 | | - * Characters will only be counted a maximum of 2 times e.g. aaa has length 2 |
91 | | - * @param $password |
92 | | - * @return int |
93 | | - */ |
94 | | - public static function getLength(string $password): int |
95 | | - { |
96 | | - $usedCharacters = []; |
97 | | - $characters = str_split($password); |
98 | | - $length = 0; |
| 88 | + /** |
| 89 | + * Check the length of the password based on known rules |
| 90 | + * Characters will only be counted a maximum of 2 times e.g. aaa has length 2 |
| 91 | + * @param $password |
| 92 | + * @return int |
| 93 | + */ |
| 94 | + public static function getLength(string $password): int |
| 95 | + { |
| 96 | + $usedCharacters = []; |
| 97 | + $characters = str_split($password); |
| 98 | + $length = 0; |
99 | 99 |
|
100 | | - foreach ($characters as $character) |
101 | | - { |
102 | | - if (array_key_exists($character, $usedCharacters) && $usedCharacters[$character] < self::$maxOccurrences) { |
103 | | - $length++; |
104 | | - $usedCharacters[$character]++; |
105 | | - } |
106 | | - if (!array_key_exists($character, $usedCharacters)) { |
107 | | - $usedCharacters[$character] = 1; |
108 | | - $length++; |
109 | | - } |
110 | | - } |
| 100 | + foreach ($characters as $character) |
| 101 | + { |
| 102 | + if (array_key_exists($character, $usedCharacters) && $usedCharacters[$character] < self::$maxOccurrences) { |
| 103 | + $length++; |
| 104 | + $usedCharacters[$character]++; |
| 105 | + } |
| 106 | + if (!array_key_exists($character, $usedCharacters)) { |
| 107 | + $usedCharacters[$character] = 1; |
| 108 | + $length++; |
| 109 | + } |
| 110 | + } |
111 | 111 |
|
112 | | - return $length; |
113 | | - } |
| 112 | + return $length; |
| 113 | + } |
114 | 114 | } |
0 commit comments