|
107 | 107 | }
|
108 | 108 |
|
109 | 109 | if (!function_exists('phpseclib_mcrypt_list_algorithms')) {
|
| 110 | + /** |
| 111 | + * Find whether the type of a variable is string (or could be converted to one) |
| 112 | + * |
| 113 | + * @param mixed $var |
| 114 | + * @return bool |
| 115 | + */ |
| 116 | + function phpseclib_is_stringable($var) |
| 117 | + { |
| 118 | + return is_string($var) || (is_object($var) && method_exists($var, '__toString')); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Returns the string length |
| 123 | + * |
| 124 | + * PHP8.1 emits a warning if $string isn't a string |
| 125 | + * |
| 126 | + * @param string $string |
| 127 | + * @return int |
| 128 | + * @access private |
| 129 | + */ |
| 130 | + function phpseclib_strlen($string) |
| 131 | + { |
| 132 | + return phpseclib_is_stringable($string) ? strlen($string) : 0; |
| 133 | + } |
| 134 | + |
110 | 135 | /**
|
111 | 136 | * Sets the key
|
112 | 137 | *
|
@@ -182,7 +207,7 @@ function phpseclib_set_iv(Base $td, $iv)
|
182 | 207 | {
|
183 | 208 | if ($td->mode != Base::MODE_ECB && $td->mode != Base::MODE_STREAM) {
|
184 | 209 | $length = $td->getBlockLength() >> 3;
|
185 |
| - $iv = str_pad(substr($iv, 0, $length), $length, "\0"); |
| 210 | + $iv = str_pad(substr(phpseclib_is_stringable($iv) ? $iv : '', 0, $length), $length, "\0"); |
186 | 211 | $td->setIV($iv);
|
187 | 212 | }
|
188 | 213 | }
|
@@ -648,10 +673,10 @@ function phpseclib_mcrypt_enc_self_test(Base $td)
|
648 | 673 | function phpseclib_mcrypt_generic_init(Base $td, $key, $iv)
|
649 | 674 | {
|
650 | 675 | $iv_size = phpseclib_mcrypt_enc_get_iv_size($td);
|
651 |
| - if (strlen($iv) != $iv_size && $td->mode != Base::MODE_ECB) { |
652 |
| - trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING); |
| 676 | + if (phpseclib_strlen($iv) != $iv_size && $td->mode != Base::MODE_ECB) { |
| 677 | + trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . phpseclib_strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING); |
653 | 678 | }
|
654 |
| - if (!strlen($key)) { |
| 679 | + if (!phpseclib_strlen($key)) { |
655 | 680 | trigger_error('mcrypt_generic_init(): Key size is 0', E_USER_WARNING);
|
656 | 681 | return -3;
|
657 | 682 | }
|
|
0 commit comments