@@ -111,12 +111,18 @@ public static function legacyDecrypt($ciphertext, $key)
111111 'Ciphertext is too short. '
112112 );
113113 }
114+ /**
115+ * @var string
116+ */
114117 $ hmac = Core::ourSubstr ($ ciphertext , 0 , Core::LEGACY_MAC_BYTE_SIZE );
115- if ($ hmac === false ) {
118+ if (! \is_string ( $ hmac) ) {
116119 throw new Ex \EnvironmentIsBrokenException ();
117120 }
121+ /**
122+ * @var string
123+ */
118124 $ ciphertext = Core::ourSubstr ($ ciphertext , Core::LEGACY_MAC_BYTE_SIZE );
119- if ($ ciphertext === false ) {
125+ if (! \is_string ( $ ciphertext) ) {
120126 throw new Ex \EnvironmentIsBrokenException ();
121127 }
122128
@@ -145,17 +151,24 @@ public static function legacyDecrypt($ciphertext, $key)
145151 'Ciphertext is too short. '
146152 );
147153 }
154+ /**
155+ * @var string
156+ */
148157 $ iv = Core::ourSubstr ($ ciphertext , 0 , Core::LEGACY_BLOCK_BYTE_SIZE );
149- if ($ iv === false ) {
158+ if (! \is_string ( $ iv) ) {
150159 throw new Ex \EnvironmentIsBrokenException ();
151160 }
152- $ ciphertext = Core::ourSubstr ($ ciphertext , Core::LEGACY_BLOCK_BYTE_SIZE );
153- if ($ ciphertext === false ) {
161+
162+ /**
163+ * @var string
164+ */
165+ $ actualCiphertext = Core::ourSubstr ($ ciphertext , Core::LEGACY_BLOCK_BYTE_SIZE );
166+ if (!\is_string ($ actualCiphertext )) {
154167 throw new Ex \EnvironmentIsBrokenException ();
155168 }
156169
157170 // Do the decryption.
158- $ plaintext = self ::plainDecrypt ($ ciphertext , $ ekey , $ iv , Core::LEGACY_CIPHER_METHOD );
171+ $ plaintext = self ::plainDecrypt ($ actualCiphertext , $ ekey , $ iv , Core::LEGACY_CIPHER_METHOD );
159172 return $ plaintext ;
160173 } else {
161174 throw new Ex \WrongKeyOrModifiedCiphertextException (
@@ -226,6 +239,7 @@ private static function decryptInternal($ciphertext, KeyOrPassword $secret, $raw
226239 }
227240
228241 // Get and check the version header.
242+ /** @var string $header */
229243 $ header = Core::ourSubstr ($ ciphertext , 0 , Core::HEADER_VERSION_SIZE );
230244 if ($ header !== Core::CURRENT_VERSION ) {
231245 throw new Ex \WrongKeyOrModifiedCiphertextException (
@@ -234,44 +248,48 @@ private static function decryptInternal($ciphertext, KeyOrPassword $secret, $raw
234248 }
235249
236250 // Get the salt.
251+ /** @var string $salt */
237252 $ salt = Core::ourSubstr (
238253 $ ciphertext ,
239254 Core::HEADER_VERSION_SIZE ,
240255 Core::SALT_BYTE_SIZE
241256 );
242- if ($ salt === false ) {
257+ if (! \is_string ( $ salt) ) {
243258 throw new Ex \EnvironmentIsBrokenException ();
244259 }
245260
246261 // Get the IV.
262+ /** @var string $iv */
247263 $ iv = Core::ourSubstr (
248264 $ ciphertext ,
249265 Core::HEADER_VERSION_SIZE + Core::SALT_BYTE_SIZE ,
250266 Core::BLOCK_BYTE_SIZE
251267 );
252- if ($ iv === false ) {
268+ if (! \is_string ( $ iv) ) {
253269 throw new Ex \EnvironmentIsBrokenException ();
254270 }
255271
256272 // Get the HMAC.
273+ /** @var string $hmac */
257274 $ hmac = Core::ourSubstr (
258275 $ ciphertext ,
259276 Core::ourStrlen ($ ciphertext ) - Core::MAC_BYTE_SIZE ,
260277 Core::MAC_BYTE_SIZE
261278 );
262- if ($ hmac === false ) {
279+ if (! \is_string ( $ hmac) ) {
263280 throw new Ex \EnvironmentIsBrokenException ();
264281 }
265282
266283 // Get the actual encrypted ciphertext.
284+ /** @var string $encrypted */
267285 $ encrypted = Core::ourSubstr (
268286 $ ciphertext ,
269287 Core::HEADER_VERSION_SIZE + Core::SALT_BYTE_SIZE +
270288 Core::BLOCK_BYTE_SIZE ,
271289 Core::ourStrlen ($ ciphertext ) - Core::MAC_BYTE_SIZE - Core::SALT_BYTE_SIZE -
272290 Core::BLOCK_BYTE_SIZE - Core::HEADER_VERSION_SIZE
273291 );
274- if ($ encrypted === false ) {
292+ if (! \is_string ( $ encrypted) ) {
275293 throw new Ex \EnvironmentIsBrokenException ();
276294 }
277295
@@ -304,6 +322,7 @@ protected static function plainEncrypt($plaintext, $key, $iv)
304322 {
305323 Core::ensureConstantExists ('OPENSSL_RAW_DATA ' );
306324 Core::ensureFunctionExists ('openssl_encrypt ' );
325+ /** @var string $ciphertext */
307326 $ ciphertext = \openssl_encrypt (
308327 $ plaintext ,
309328 Core::CIPHER_METHOD ,
@@ -312,7 +331,7 @@ protected static function plainEncrypt($plaintext, $key, $iv)
312331 $ iv
313332 );
314333
315- if ($ ciphertext === false ) {
334+ if (! \is_string ( $ ciphertext) ) {
316335 throw new Ex \EnvironmentIsBrokenException (
317336 'openssl_encrypt() failed. '
318337 );
@@ -337,14 +356,16 @@ protected static function plainDecrypt($ciphertext, $key, $iv, $cipherMethod)
337356 {
338357 Core::ensureConstantExists ('OPENSSL_RAW_DATA ' );
339358 Core::ensureFunctionExists ('openssl_decrypt ' );
359+
360+ /** @var string $plaintext */
340361 $ plaintext = \openssl_decrypt (
341362 $ ciphertext ,
342363 $ cipherMethod ,
343364 $ key ,
344365 OPENSSL_RAW_DATA ,
345366 $ iv
346367 );
347- if ($ plaintext === false ) {
368+ if (! \is_string ( $ plaintext) ) {
348369 throw new Ex \EnvironmentIsBrokenException (
349370 'openssl_decrypt() failed. '
350371 );
0 commit comments