Skip to content

Commit 193f62a

Browse files
committed
add crypto prefix
1 parent 9234e16 commit 193f62a

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/Cryptography/Cipher/OpensslCipher.php

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

1818
final class OpensslCipher implements Cipher
1919
{
20+
public function __construct(
21+
private readonly string $prefix = '',
22+
) {
23+
}
24+
2025
public function encrypt(CipherKey $key, mixed $data): string
2126
{
27+
if ($this->prefix && is_string($data) && str_starts_with($data, $this->prefix)) {
28+
throw new EncryptionFailed();
29+
}
30+
2231
$encryptedData = @openssl_encrypt(
2332
$this->dataEncode($data),
2433
$key->method,
@@ -31,11 +40,19 @@ public function encrypt(CipherKey $key, mixed $data): string
3140
throw new EncryptionFailed();
3241
}
3342

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

3746
public function decrypt(CipherKey $key, string $data): mixed
3847
{
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+
3956
$data = @openssl_decrypt(
4057
base64_decode($data),
4158
$key->method,

0 commit comments

Comments
 (0)