Skip to content

Commit 24f2efc

Browse files
committed
use constants for array keys
1 parent 3e9e1e8 commit 24f2efc

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

src/Extension/Cryptography/BaseCryptographer.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
*/
3030
final class BaseCryptographer implements Cryptographer
3131
{
32+
private const VERSION_KEY = 'v';
33+
private const METHOD_KEY = 'a';
34+
private const KEY_ID_KEY = 'k';
35+
private const NONCE_KEY = 'n';
36+
private const DATA_KEY = 'd';
37+
private const TAG_KEY = 't';
38+
3239
public function __construct(
3340
private readonly Cipher $cipher,
3441
private readonly CipherKeyStore $cipherKeyStore,
@@ -53,18 +60,18 @@ public function encrypt(string $subjectId, mixed $value): array
5360
$parameter = $this->cipher->encrypt($cipherKey, $value);
5461

5562
$result = [
56-
'v' => 1,
57-
'a' => $parameter->method,
58-
'k' => $cipherKey->id,
59-
'd' => $parameter->data,
63+
self::VERSION_KEY => 1,
64+
self::METHOD_KEY => $parameter->method,
65+
self::KEY_ID_KEY => $cipherKey->id,
66+
self::DATA_KEY => $parameter->data,
6067
];
6168

6269
if ($parameter->nonce !== null) {
63-
$result['n'] = $parameter->nonce;
70+
$result[self::NONCE_KEY] = $parameter->nonce;
6471
}
6572

6673
if ($parameter->tag !== null) {
67-
$result['t'] = $parameter->tag;
74+
$result[self::TAG_KEY] = $parameter->tag;
6875
}
6976

7077
return $result;
@@ -78,7 +85,7 @@ public function encrypt(string $subjectId, mixed $value): array
7885
*/
7986
public function decrypt(string $subjectId, mixed $encryptedData): mixed
8087
{
81-
$keyId = $encryptedData['k'] ?? null;
88+
$keyId = $encryptedData[self::KEY_ID_KEY] ?? null;
8289

8390
if ($keyId === null) {
8491
throw DecryptionFailed::missingKeyId();
@@ -89,19 +96,19 @@ public function decrypt(string $subjectId, mixed $encryptedData): mixed
8996
return $this->cipher->decrypt(
9097
$cipherKey,
9198
new EncryptedData(
92-
$encryptedData['d'],
93-
$encryptedData['a'],
94-
$encryptedData['n'] ?? null,
95-
$encryptedData['t'] ?? null,
99+
$encryptedData[self::DATA_KEY],
100+
$encryptedData[self::METHOD_KEY],
101+
$encryptedData[self::NONCE_KEY] ?? null,
102+
$encryptedData[self::TAG_KEY] ?? null,
96103
),
97104
);
98105
}
99106

100107
public function supports(mixed $value): bool
101108
{
102109
return is_array($value)
103-
&& isset($value['v'], $value['a'], $value['k'], $value['d'])
104-
&& $value['v'] === 1;
110+
&& isset($value[self::VERSION_KEY], $value[self::METHOD_KEY], $value[self::KEY_ID_KEY], $value[self::DATA_KEY])
111+
&& $value[self::VERSION_KEY] === 1;
105112
}
106113

107114
/** @param non-empty-string $method */

0 commit comments

Comments
 (0)