Skip to content

Commit ac14cc2

Browse files
committed
move logic into personal data payload cryptographer
1 parent bf97229 commit ac14cc2

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

src/Cryptography/Cipher/OpensslCipher.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,8 @@
1717

1818
final class OpensslCipher implements Cipher
1919
{
20-
public function __construct(
21-
private readonly string $prefix = '',
22-
) {
23-
}
24-
2520
public function encrypt(CipherKey $key, mixed $data): string
2621
{
27-
if ($this->prefix && is_string($data) && str_starts_with($data, $this->prefix)) {
28-
throw new EncryptionFailed();
29-
}
30-
3122
$encryptedData = @openssl_encrypt(
3223
$this->dataEncode($data),
3324
$key->method,
@@ -40,19 +31,11 @@ public function encrypt(CipherKey $key, mixed $data): string
4031
throw new EncryptionFailed();
4132
}
4233

43-
return $this->prefix . base64_encode($encryptedData);
34+
return base64_encode($encryptedData);
4435
}
4536

4637
public function decrypt(CipherKey $key, string $data): mixed
4738
{
48-
if ($this->prefix) {
49-
if (str_starts_with($data, $this->prefix)) {
50-
$data = substr($data, strlen($this->prefix));
51-
} else {
52-
return $data;
53-
}
54-
}
55-
5639
$data = @openssl_decrypt(
5740
base64_decode($data),
5841
$key->method,

src/Cryptography/PersonalDataPayloadCryptographer.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323
private readonly CipherKeyStore $cipherKeyStore,
2424
private readonly CipherKeyFactory $cipherKeyFactory,
2525
private readonly Cipher $cipher,
26+
private readonly string $encryptedDataPrefix = '',
2627
) {
2728
}
2829

@@ -51,7 +52,7 @@ public function encrypt(ClassMetadata $metadata, array $data): array
5152
continue;
5253
}
5354

54-
$data[$propertyMetadata->fieldName()] = $this->cipher->encrypt(
55+
$data[$propertyMetadata->fieldName()] = $this->encryptedDataPrefix . $this->cipher->encrypt(
5556
$cipherKey,
5657
$data[$propertyMetadata->fieldName()],
5758
);
@@ -84,6 +85,16 @@ public function decrypt(ClassMetadata $metadata, array $data): array
8485
continue;
8586
}
8687

88+
$fieldData = $data[$propertyMetadata->fieldName()];
89+
90+
if ($this->encryptedDataPrefix !== '') {
91+
if (str_starts_with($fieldData, $this->encryptedDataPrefix)) {
92+
$fieldData = mb_substr($fieldData, mb_strlen($this->encryptedDataPrefix));
93+
} else {
94+
continue;
95+
}
96+
}
97+
8798
if (!$cipherKey) {
8899
$data[$propertyMetadata->fieldName()] = $propertyMetadata->personalDataFallback();
89100
continue;
@@ -92,7 +103,7 @@ public function decrypt(ClassMetadata $metadata, array $data): array
92103
try {
93104
$data[$propertyMetadata->fieldName()] = $this->cipher->decrypt(
94105
$cipherKey,
95-
$data[$propertyMetadata->fieldName()],
106+
$fieldData,
96107
);
97108
} catch (DecryptionFailed) {
98109
$data[$propertyMetadata->fieldName()] = $propertyMetadata->personalDataFallback();

0 commit comments

Comments
 (0)