Skip to content

Commit 163b37b

Browse files
authored
Merge pull request #155 from patchlevel/base64-iv
base64 iv in payload
2 parents 2e6edbf + 021513c commit 163b37b

3 files changed

Lines changed: 13 additions & 41 deletions

File tree

phpstan-baseline.neon

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: '#^Parameter \#3 \$iv of class Patchlevel\\Hydrator\\Extension\\Cryptography\\Cipher\\CipherKey constructor expects non\-empty\-string, string given\.$#'
5+
identifier: argument.type
6+
count: 1
7+
path: src/Extension/Cryptography/BaseCryptographer.php
8+
39
-
410
message: '#^Method Patchlevel\\Hydrator\\Extension\\Cryptography\\Cipher\\OpensslCipher\:\:encrypt\(\) should return non\-empty\-string but returns string\.$#'
511
identifier: return.type
@@ -90,42 +96,6 @@ parameters:
9096
count: 1
9197
path: src/Normalizer/ObjectNormalizer.php
9298

93-
-
94-
message: '#^Call to method clear\(\) on an unknown class Patchlevel\\Hydrator\\Cryptography\\Store\\InMemoryCipherKeyStore\.$#'
95-
identifier: class.notFound
96-
count: 1
97-
path: tests/Benchmark/HydratorWithCryptographyBench.php
98-
99-
-
100-
message: '#^Call to static method createWithOpenssl\(\) on an unknown class Patchlevel\\Hydrator\\Cryptography\\BaseCryptographer\.$#'
101-
identifier: class.notFound
102-
count: 1
103-
path: tests/Benchmark/HydratorWithCryptographyBench.php
104-
105-
-
106-
message: '#^Instantiated class Patchlevel\\Hydrator\\Cryptography\\CryptographyExtension not found\.$#'
107-
identifier: class.notFound
108-
count: 1
109-
path: tests/Benchmark/HydratorWithCryptographyBench.php
110-
111-
-
112-
message: '#^Instantiated class Patchlevel\\Hydrator\\Cryptography\\Store\\InMemoryCipherKeyStore not found\.$#'
113-
identifier: class.notFound
114-
count: 1
115-
path: tests/Benchmark/HydratorWithCryptographyBench.php
116-
117-
-
118-
message: '#^Parameter \#1 \$extension of method Patchlevel\\Hydrator\\HydratorBuilder\:\:useExtension\(\) expects Patchlevel\\Hydrator\\Extension, Patchlevel\\Hydrator\\Cryptography\\CryptographyExtension given\.$#'
119-
identifier: argument.type
120-
count: 1
121-
path: tests/Benchmark/HydratorWithCryptographyBench.php
122-
123-
-
124-
message: '#^Property Patchlevel\\Hydrator\\Tests\\Benchmark\\HydratorWithCryptographyBench\:\:\$store has unknown class Patchlevel\\Hydrator\\Cryptography\\Store\\InMemoryCipherKeyStore as its type\.$#'
125-
identifier: class.notFound
126-
count: 1
127-
path: tests/Benchmark/HydratorWithCryptographyBench.php
128-
12999
-
130100
message: '#^Property Patchlevel\\Hydrator\\Tests\\Unit\\Extension\\Cryptography\\Fixture\\ChildWithSensitiveDataWithIdentifierDto\:\:\$email is never read, only written\.$#'
131101
identifier: property.onlyWritten

src/Extension/Cryptography/BaseCryptographer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyStore;
1616

1717
use function array_key_exists;
18+
use function base64_decode;
19+
use function base64_encode;
1820
use function is_array;
1921

2022
/**
@@ -52,7 +54,7 @@ public function encrypt(string $subjectId, mixed $value): array
5254
'__enc' => 'v1',
5355
'data' => $this->cipher->encrypt($cipherKey, $value),
5456
'method' => $cipherKey->method,
55-
'iv' => $cipherKey->iv,
57+
'iv' => base64_encode($cipherKey->iv),
5658
];
5759
}
5860

@@ -70,7 +72,7 @@ public function decrypt(string $subjectId, mixed $encryptedData): mixed
7072
new CipherKey(
7173
$cipherKey->key,
7274
$encryptedData['method'] ?? $cipherKey->method,
73-
$encryptedData['iv'] ?? $cipherKey->iv,
75+
isset($encryptedData['iv']) ? base64_decode($encryptedData['iv']) : $cipherKey->iv,
7476
),
7577
$encryptedData['data'],
7678
);

tests/Unit/Extension/Cryptography/BaseCryptographerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testEncrypt(): void
4646
'__enc' => 'v1',
4747
'data' => 'encrypted',
4848
'method' => 'methodA',
49-
'iv' => 'random',
49+
'iv' => 'cmFuZG9t',
5050
];
5151

5252
self::assertEquals($expected, $cryptographer->encrypt('foo', 'info@patchlevel.de'));
@@ -81,7 +81,7 @@ public function testEncryptWithoutKey(): void
8181
'__enc' => 'v1',
8282
'data' => 'encrypted',
8383
'method' => 'methodA',
84-
'iv' => 'random',
84+
'iv' => 'cmFuZG9t',
8585
];
8686

8787
self::assertEquals($expected, $cryptographer->encrypt('foo', 'info@patchlevel.de'));
@@ -115,7 +115,7 @@ public function testDecrypt(): void
115115
'__enc' => 'v1',
116116
'data' => 'encrypted',
117117
'method' => 'methodA',
118-
'iv' => 'random',
118+
'iv' => 'cmFuZG9t',
119119
],
120120
),
121121
);

0 commit comments

Comments
 (0)