File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed
Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 1717
1818final 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 ,
You can’t perform that action at this time.
0 commit comments