Skip to content

Commit 8a9f9f0

Browse files
committed
Merge branch '1.0' into 2.0
2 parents 05b5db2 + e38b76f commit 8a9f9f0

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/mcrypt.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use phpseclib3\Crypt\RC4;
4141
use phpseclib3\Crypt\Random;
4242
use phpseclib3\Crypt\Common\SymmetricKey as Base;
43+
use phpseclib3\Common\Functions\Strings;
4344

4445
if (!defined('MCRYPT_MODE_ECB')) {
4546
/**#@+
@@ -107,6 +108,20 @@
107108
}
108109

109110
if (!function_exists('phpseclib_mcrypt_list_algorithms')) {
111+
/**
112+
* Returns the string length
113+
*
114+
* PHP8.1 emits a warning if $string isn't a string
115+
*
116+
* @param string $string
117+
* @return int
118+
* @access private
119+
*/
120+
function phpseclib_strlen($string)
121+
{
122+
return Strings::is_stringable($string) ? strlen($string) : 0;
123+
}
124+
110125
/**
111126
* Sets the key
112127
*
@@ -182,7 +197,7 @@ function phpseclib_set_iv(Base $td, $iv)
182197
{
183198
if ($td->getMode() != 'ecb' && $td->getMode() != 'stream') {
184199
$length = $td->getBlockLength() >> 3;
185-
$iv = str_pad(substr($iv, 0, $length), $length, "\0");
200+
$iv = str_pad(substr(Strings::is_stringable($iv) ? $iv : '', 0, $length), $length, "\0");
186201
$td->setIV($iv);
187202
}
188203
}
@@ -647,10 +662,10 @@ function phpseclib_mcrypt_enc_self_test(Base $td)
647662
function phpseclib_mcrypt_generic_init(Base $td, $key, $iv)
648663
{
649664
$iv_size = phpseclib_mcrypt_enc_get_iv_size($td);
650-
if (strlen($iv) != $iv_size && $td->getMode() != 'ecb') {
651-
trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING);
665+
if (phpseclib_strlen($iv) != $iv_size && $td->getMode() != 'ecb') {
666+
trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . phpseclib_strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING);
652667
}
653-
if (!strlen($key)) {
668+
if (!phpseclib_strlen($key)) {
654669
trigger_error('mcrypt_generic_init(): Key size is 0', E_USER_WARNING);
655670
return -3;
656671
}

0 commit comments

Comments
 (0)