-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPassword.php
208 lines (197 loc) · 5.83 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
<?php declare(strict_types=1);
/*
* This file is part of Aplus Framework Crypto Library.
*
* (c) Natan Felles <natanfelles@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Framework\Crypto;
use InvalidArgumentException;
use SensitiveParameter;
use SodiumException;
/**
* Class Password.
*
* @package crypto
*/
class Password
{
/**
* Used to set operations or memory limit as interactive.
* It enables the use of 2 CPU operations or 64 MB RAM.
*/
public const int LIMIT_INTERACTIVE = 0;
/**
* Used to set operations or memory limit as moderate.
* It enables the use of 3 CPU operations or 256 MB RAM.
*/
public const int LIMIT_MODERATE = 1;
/**
* Used to set operations or memory limit as sensitive.
* It enables the use of 4 CPU operations or 1 GB RAM.
*/
public const int LIMIT_SENSITIVE = 2;
protected static int $opsLimit = Password::LIMIT_INTERACTIVE;
protected static int $memLimit = Password::LIMIT_INTERACTIVE;
/**
* Makes a password hash.
*
* @param string $password
* @param int|null $opslimit A Password constant or null to use the default
* set for opslimit
* @param int|null $memlimit A Password constant or null to use the default
* set for memlimit. Typically, it should be paired with the opslimit value
*
* @see Password::LIMIT_INTERACTIVE
* @see Password::LIMIT_MODERATE
* @see Password::LIMIT_SENSITIVE
*
* @throws SodiumException
*
* @return string
*/
public static function hash(
#[SensitiveParameter]
string $password,
?int $opslimit = null,
?int $memlimit = null
) : string {
$opslimit ??= static::getOpsLimit();
$memlimit ??= static::getMemLimit();
return \sodium_crypto_pwhash_str(
$password,
static::getSodiumOpsLimit($opslimit),
static::getSodiumMemLimit($memlimit)
);
}
/**
* Checks if a hash needs to be rehashed based on the ops and mem limits.
*
* @param string $hash
* @param int|null $opslimit A Password constant or null to use the default
* set for opslimit
* @param int|null $memlimit A Password constant or null to use the default
* set for memlimit
*
* @return bool
*/
public static function needsRehash(
#[SensitiveParameter]
string $hash,
?int $opslimit = null,
?int $memlimit = null
) : bool {
$opslimit ??= static::getOpsLimit();
$memlimit ??= static::getMemLimit();
return \sodium_crypto_pwhash_str_needs_rehash(
$hash,
static::getSodiumOpsLimit($opslimit),
static::getSodiumMemLimit($memlimit)
);
}
/**
* Verifies a password against a hash.
*
* @param string $password
* @param string $hash
*
* @throws SodiumException
*
* @return bool
*/
public static function verify(
#[SensitiveParameter]
string $password,
#[SensitiveParameter]
string $hash
) : bool {
return \sodium_crypto_pwhash_str_verify($hash, $password);
}
/**
* Sets the default Password operations limit.
*
* @param int $opsLimit A Password constant value
*
* @see Password::LIMIT_INTERACTIVE
* @see Password::LIMIT_MODERATE
* @see Password::LIMIT_SENSITIVE
*/
public static function setOpsLimit(int $opsLimit) : void
{
static::$opsLimit = $opsLimit;
}
/**
* Gets the default Password operations limit constant value.
*
* @return int
*/
public static function getOpsLimit() : int
{
return static::$opsLimit;
}
/**
* Sets the default Password memory limit.
*
* @param int $memLimit A Password constant value. Typically, it should be
* paired with the opslimit value
*
* @see Password::LIMIT_INTERACTIVE
* @see Password::LIMIT_MODERATE
* @see Password::LIMIT_SENSITIVE
*/
public static function setMemLimit(int $memLimit) : void
{
static::$memLimit = $memLimit;
}
/**
* Gets the default Password memory limit constant value.
*
* @return int
*/
public static function getMemLimit() : int
{
return static::$memLimit;
}
/**
* Gets an appropriate sodium operations limit value from a Password constant.
*
* @param int $constant
*
* @throws InvalidArgumentException if constant value is invalid
*
* @return int
*/
protected static function getSodiumOpsLimit(int $constant) : int
{
return match ($constant) {
static::LIMIT_INTERACTIVE => \SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
static::LIMIT_MODERATE => \SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE,
static::LIMIT_SENSITIVE => \SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE,
default => throw new InvalidArgumentException(
'Invalid opslimit value: ' . $constant
)
};
}
/**
* Gets an appropriate sodium memory limit value from a Password constant.
*
* @param int $constant
*
* @throws InvalidArgumentException if constant value is invalid
*
* @return int
*/
protected static function getSodiumMemLimit(int $constant) : int
{
return match ($constant) {
static::LIMIT_INTERACTIVE => \SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE,
static::LIMIT_MODERATE => \SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE,
static::LIMIT_SENSITIVE => \SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE,
default => throw new InvalidArgumentException(
'Invalid memlimit value: ' . $constant
)
};
}
}