|
40 | 40 | use phpseclib3\Crypt\RC4;
|
41 | 41 | use phpseclib3\Crypt\Random;
|
42 | 42 | use phpseclib3\Crypt\Common\SymmetricKey as Base;
|
| 43 | +use phpseclib3\Common\Functions\Strings; |
43 | 44 |
|
44 | 45 | if (!defined('MCRYPT_MODE_ECB')) {
|
45 | 46 | /**#@+
|
|
107 | 108 | }
|
108 | 109 |
|
109 | 110 | 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 | + |
110 | 125 | /**
|
111 | 126 | * Sets the key
|
112 | 127 | *
|
@@ -182,7 +197,7 @@ function phpseclib_set_iv(Base $td, $iv)
|
182 | 197 | {
|
183 | 198 | if ($td->getMode() != 'ecb' && $td->getMode() != 'stream') {
|
184 | 199 | $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"); |
186 | 201 | $td->setIV($iv);
|
187 | 202 | }
|
188 | 203 | }
|
@@ -647,10 +662,10 @@ function phpseclib_mcrypt_enc_self_test(Base $td)
|
647 | 662 | function phpseclib_mcrypt_generic_init(Base $td, $key, $iv)
|
648 | 663 | {
|
649 | 664 | $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); |
652 | 667 | }
|
653 |
| - if (!strlen($key)) { |
| 668 | + if (!phpseclib_strlen($key)) { |
654 | 669 | trigger_error('mcrypt_generic_init(): Key size is 0', E_USER_WARNING);
|
655 | 670 | return -3;
|
656 | 671 | }
|
|
0 commit comments