Skip to content

Commit 292f4e0

Browse files
authored
Add SensitiveParameter to secrets (#62)
1 parent c9e5f58 commit 292f4e0

5 files changed

Lines changed: 72 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 1.0.3 under development
44

5-
- no changes in this release.
5+
- Enh #62: Use `SensitiveParameter` attribute to mark sensitive parameters (@dehbka, @vjik)
66

77
## 1.0.2 March 18, 2024
88

src/Crypt.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Security;
66

7+
use SensitiveParameter;
78
use Yiisoft\Strings\StringHelper;
89

910
final class Crypt
@@ -123,8 +124,11 @@ public function withDerivationIterations(int $iterations): self
123124
* @see decryptByPassword()
124125
* @see encryptByKey()
125126
*/
126-
public function encryptByPassword(string $data, string $password): string
127-
{
127+
public function encryptByPassword(
128+
string $data,
129+
#[SensitiveParameter]
130+
string $password
131+
): string {
128132
return $this->encrypt($data, true, $password, '');
129133
}
130134

@@ -150,8 +154,12 @@ public function encryptByPassword(string $data, string $password): string
150154
* @see decryptByKey()
151155
* @see encryptByPassword()
152156
*/
153-
public function encryptByKey(string $data, string $inputKey, string $info = ''): string
154-
{
157+
public function encryptByKey(
158+
string $data,
159+
#[SensitiveParameter]
160+
string $inputKey,
161+
string $info = ''
162+
): string {
155163
return $this->encrypt($data, false, $inputKey, $info);
156164
}
157165

@@ -169,8 +177,11 @@ public function encryptByKey(string $data, string $inputKey, string $info = ''):
169177
*
170178
* @see encryptByPassword()
171179
*/
172-
public function decryptByPassword(string $data, string $password): string
173-
{
180+
public function decryptByPassword(
181+
string $data,
182+
#[SensitiveParameter]
183+
string $password
184+
): string {
174185
return $this->decrypt($data, true, $password, '');
175186
}
176187

@@ -190,8 +201,12 @@ public function decryptByPassword(string $data, string $password): string
190201
*
191202
* @see encryptByKey()
192203
*/
193-
public function decryptByKey(string $data, string $inputKey, string $info = ''): string
194-
{
204+
public function decryptByKey(
205+
string $data,
206+
#[SensitiveParameter]
207+
string $inputKey,
208+
string $info = ''
209+
): string {
195210
return $this->decrypt($data, false, $inputKey, $info);
196211
}
197212

@@ -211,8 +226,13 @@ public function decryptByKey(string $data, string $inputKey, string $info = ''):
211226
*
212227
* @see decrypt()
213228
*/
214-
private function encrypt(string $data, bool $passwordBased, string $secret, string $info = ''): string
215-
{
229+
private function encrypt(
230+
string $data,
231+
bool $passwordBased,
232+
#[SensitiveParameter]
233+
string $secret,
234+
string $info = ''
235+
): string {
216236
[$blockSize, $keySize] = self::ALLOWED_CIPHERS[$this->cipher];
217237

218238
$keySalt = random_bytes($keySize);
@@ -257,8 +277,13 @@ private function encrypt(string $data, bool $passwordBased, string $secret, stri
257277
*
258278
* @see encrypt()
259279
*/
260-
private function decrypt(string $data, bool $passwordBased, string $secret, string $info): string
261-
{
280+
private function decrypt(
281+
string $data,
282+
bool $passwordBased,
283+
#[SensitiveParameter]
284+
string $secret,
285+
string $info
286+
): string {
262287
[$blockSize, $keySize] = self::ALLOWED_CIPHERS[$this->cipher];
263288

264289
$keySalt = StringHelper::byteSubstring($data, 0, $keySize);

src/Mac.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Security;
66

7+
use SensitiveParameter;
78
use Yiisoft\Strings\StringHelper;
89

910
/**
@@ -51,8 +52,12 @@ public function __construct(string $algorithm = 'sha256')
5152
* @see hkdf()
5253
* @see pbkdf2()
5354
*/
54-
public function sign(string $data, string $key, bool $rawHash = false): string
55-
{
55+
public function sign(
56+
string $data,
57+
#[SensitiveParameter]
58+
string $key,
59+
bool $rawHash = false
60+
): string {
5661
$hash = hash_hmac($this->algorithm, $data, $key, $rawHash);
5762
if (!$hash) {
5863
throw new \RuntimeException("Failed to generate HMAC with hash algorithm: {$this->algorithm}.");
@@ -80,8 +85,12 @@ public function sign(string $data, string $key, bool $rawHash = false): string
8085
*
8186
* @see hash()
8287
*/
83-
public function getMessage(string $data, string $key, bool $rawHash = false): string
84-
{
88+
public function getMessage(
89+
string $data,
90+
#[SensitiveParameter]
91+
string $key,
92+
bool $rawHash = false
93+
): string {
8594
$test = hash_hmac($this->algorithm, '', '', $rawHash);
8695
if (!$test) {
8796
throw new \RuntimeException("Failed to generate HMAC with hash algorithm: {$this->algorithm}.");

src/PasswordHasher.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yiisoft\Security;
66

7+
use SensitiveParameter;
8+
79
/**
810
* PasswordHasher allows generating password hash and verifying passwords against a hash.
911
*/
@@ -65,8 +67,10 @@ public function __construct(?string $algorithm = PASSWORD_DEFAULT, array $parame
6567
* @psalm-suppress InvalidNullableReturnType
6668
* @psalm-suppress NullableReturnStatement
6769
*/
68-
public function hash(string $password): string
69-
{
70+
public function hash(
71+
#[SensitiveParameter]
72+
string $password
73+
): string {
7074
return password_hash($password, $this->algorithm, $this->parameters);
7175
}
7276

@@ -83,8 +87,12 @@ public function hash(string $password): string
8387
*
8488
* @see hash()
8589
*/
86-
public function validate(string $password, string $hash): bool
87-
{
90+
public function validate(
91+
#[SensitiveParameter]
92+
string $password,
93+
#[SensitiveParameter]
94+
string $hash
95+
): bool {
8896
if ($password === '') {
8997
throw new \InvalidArgumentException('Password must be a string and cannot be empty.');
9098
}

src/TokenMask.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Security;
66

7+
use SensitiveParameter;
78
use Yiisoft\Strings\StringHelper;
89

910
/**
@@ -22,8 +23,10 @@ final class TokenMask
2223
*
2324
* @return string A masked token.
2425
*/
25-
public static function apply(string $token): string
26-
{
26+
public static function apply(
27+
#[SensitiveParameter]
28+
string $token
29+
): string {
2730
// The number of bytes in a mask is always equal to the number of bytes in a token.
2831
/** @psalm-suppress ArgumentTypeCoercion */
2932
$mask = random_bytes(StringHelper::byteLength($token));
@@ -37,8 +40,10 @@ public static function apply(string $token): string
3740
*
3841
* @return string An unmasked token, or an empty string in case of token format is invalid.
3942
*/
40-
public static function remove(string $maskedToken): string
41-
{
43+
public static function remove(
44+
#[SensitiveParameter]
45+
string $maskedToken
46+
): string {
4247
$decoded = StringHelper::base64UrlDecode($maskedToken);
4348
$length = StringHelper::byteLength($decoded) / 2;
4449
// Check if the masked token has an even length.

0 commit comments

Comments
 (0)