Skip to content

Commit

Permalink
fix PHP 8.1 deprecations when $iv is null
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Mar 27, 2022
1 parent c1f0014 commit e38b76f
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions lib/mcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down

1 comment on commit e38b76f

@terrafrost
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #34

Please sign in to comment.