@@ -159,3 +159,36 @@ function preg_replace($pattern, $replacement, $subject, int $limit = -1, int &$c
159
159
}
160
160
return $ result ;
161
161
}
162
+
163
+ /**
164
+ * Encrypts given data with given method and key, returns a raw
165
+ * or base64 encoded string
166
+ *
167
+ * @param string $data The plaintext message data to be encrypted.
168
+ * @param string $method The cipher method. For a list of available cipher methods, use openssl_get_cipher_methods.
169
+ * @param string $key The key.
170
+ * @param int $options options is a bitwise disjunction of the flags
171
+ * OPENSSL_RAW_DATA and
172
+ * OPENSSL_ZERO_PADDING.
173
+ * @param string $iv A non-NULL Initialization Vector.
174
+ * @param string $tag The authentication tag passed by reference when using AEAD cipher mode (GCM or CCM).
175
+ * @param string $aad Additional authentication data.
176
+ * @param int $tag_length The length of the authentication tag. Its value can be between 4 and 16 for GCM mode.
177
+ * @return string Returns the encrypted string.
178
+ * @throws OpensslException
179
+ *
180
+ */
181
+ function openssl_encrypt (string $ data , string $ method , string $ key , int $ options = 0 , string $ iv = "" , string &$ tag = null , string $ aad = "" , int $ tag_length = 16 ): string
182
+ {
183
+ error_clear_last ();
184
+ // The $tag parameter is handled in a weird way by openssl_encrypt. It cannot be provided unless encoding is AEAD
185
+ if (func_num_args () <= 5 ) {
186
+ $ result = \openssl_encrypt ($ data , $ method , $ key , $ options , $ iv );
187
+ } else {
188
+ $ result = \openssl_encrypt ($ data , $ method , $ key , $ options , $ iv , $ tag , $ aad , $ tag_length );
189
+ }
190
+ if ($ result === false ) {
191
+ throw OpensslException::createFromPhpError ();
192
+ }
193
+ return $ result ;
194
+ }
0 commit comments