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