From e38b76f02e6cf97aca05f5738eee1b917d922101 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 27 Mar 2022 10:50:21 -0500 Subject: [PATCH] fix PHP 8.1 deprecations when $iv is null --- lib/mcrypt.php | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/mcrypt.php b/lib/mcrypt.php index 01dda15..bb426d5 100644 --- a/lib/mcrypt.php +++ b/lib/mcrypt.php @@ -107,6 +107,31 @@ } if (!function_exists('phpseclib_mcrypt_list_algorithms')) { + /** + * Find whether the type of a variable is string (or could be converted to one) + * + * @param mixed $var + * @return bool + */ + function phpseclib_is_stringable($var) + { + return is_string($var) || (is_object($var) && method_exists($var, '__toString')); + } + + /** + * Returns the string length + * + * PHP8.1 emits a warning if $string isn't a string + * + * @param string $string + * @return int + * @access private + */ + function phpseclib_strlen($string) + { + return phpseclib_is_stringable($string) ? strlen($string) : 0; + } + /** * Sets the key * @@ -182,7 +207,7 @@ function phpseclib_set_iv(Base $td, $iv) { if ($td->mode != Base::MODE_ECB && $td->mode != Base::MODE_STREAM) { $length = $td->getBlockLength() >> 3; - $iv = str_pad(substr($iv, 0, $length), $length, "\0"); + $iv = str_pad(substr(phpseclib_is_stringable($iv) ? $iv : '', 0, $length), $length, "\0"); $td->setIV($iv); } } @@ -648,10 +673,10 @@ function phpseclib_mcrypt_enc_self_test(Base $td) function phpseclib_mcrypt_generic_init(Base $td, $key, $iv) { $iv_size = phpseclib_mcrypt_enc_get_iv_size($td); - if (strlen($iv) != $iv_size && $td->mode != Base::MODE_ECB) { - trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING); + if (phpseclib_strlen($iv) != $iv_size && $td->mode != Base::MODE_ECB) { + trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . phpseclib_strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING); } - if (!strlen($key)) { + if (!phpseclib_strlen($key)) { trigger_error('mcrypt_generic_init(): Key size is 0', E_USER_WARNING); return -3; }