1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Validate; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Validate\Traits\BlockStringTrait; |
7
|
|
|
use Validate\Traits\GetDataTrait; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* I created this validator just to avoid common passwords, you should not use to store passwords. |
11
|
|
|
* Not before you improve that shit! |
12
|
|
|
* |
13
|
|
|
* Criei esse validator apenas para evitar senhas comuns, você não deveria usar para armazenar senhas. |
14
|
|
|
* Não antes de melhorar essa merda! |
15
|
|
|
*/ |
16
|
|
|
class Password implements \Validate\Contracts\Validate |
17
|
|
|
{ |
18
|
|
|
use BlockStringTrait, GetDataTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create hash for store password |
22
|
|
|
* |
23
|
|
|
* @param string $password |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public static function toDatabase(string $password) |
27
|
|
|
{ |
28
|
|
|
return sha256($password); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Never use this function |
33
|
|
|
* |
34
|
|
|
* @param string $password |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public static function toUser($password) |
38
|
|
|
{ |
39
|
|
|
// @todo Nem deveria existir isso aqui ! |
40
|
|
|
die(); |
|
|
|
|
41
|
|
|
return null; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Verify if client use commoms passwords. |
46
|
|
|
* |
47
|
|
|
* @todo Create validate for password force |
48
|
|
|
* |
49
|
|
|
* @param string $password |
50
|
|
|
* @return boolean |
51
|
|
|
*/ |
52
|
|
|
public static function validate(string $password): boolean |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
if (self::foundInMultiplesArrays( |
55
|
|
|
[ |
56
|
|
|
[ |
57
|
|
|
$password, |
58
|
|
|
self::getListFromFile('black-passwords') |
59
|
|
|
], |
60
|
|
|
[ |
61
|
|
|
$password, |
62
|
|
|
self::getListFromFile('black-names') |
63
|
|
|
], |
64
|
|
|
[ |
65
|
|
|
$password, |
66
|
|
|
self::getListFromFile('black-first-names') |
67
|
|
|
], |
68
|
|
|
] |
69
|
|
|
) |
70
|
|
|
) { |
71
|
|
|
return false; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return true; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Verify |
79
|
|
|
* |
80
|
|
|
* @param string $fromDatabase |
81
|
|
|
* @param string $fromUser |
82
|
|
|
* @return boolean |
83
|
|
|
*/ |
84
|
|
|
public static function isSame(string $fromDatabase, string $fromUser) |
85
|
|
|
{ |
86
|
|
|
return (self::toDatabase($fromDatabase)===self::toDatabase($fromUser)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function generate( |
90
|
|
|
$length = 8, |
91
|
|
|
$keyspace = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%' |
92
|
|
|
) { |
93
|
|
|
$str = ''; |
94
|
|
|
$max = mb_strlen($keyspace, '8bit') - 1; |
95
|
|
|
if ($max < 1) { |
96
|
|
|
throw new Exception('$keyspace must be at least two characters long'); |
97
|
|
|
} |
98
|
|
|
for ($i = 0; $i < $length; ++$i) { |
99
|
|
|
$str .= $keyspace[random_int(0, $max)]; |
100
|
|
|
} |
101
|
|
|
return $str; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|