From 28866e826b30891c043b2136d89a67520a5d338c Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 8 Sep 2024 07:18:47 -0500 Subject: [PATCH] Hash: fix for masks at upper boundary of what signed ints allow on 64-bit PHP installs (1 << 63) - 1 == (1 << 63) -1 ^ (-1 << 63) gives the correct result on 32-bit PHP installs (1 << 31) - 1 returns a float and using the result as a bitmask yields this error on sufficiently new versions of PHP: Deprecated: Implicit conversion from float -2147483649 to int loses precision Explicitly casting (1 << 31) - 1 to an int yields the correct result but, then again, so does -1 ^ (-1 << 31) and that one is consistent with how it works on 64-bit PHP installs --- phpseclib/Crypt/Hash.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/phpseclib/Crypt/Hash.php b/phpseclib/Crypt/Hash.php index 2d01807b..4825c70d 100644 --- a/phpseclib/Crypt/Hash.php +++ b/phpseclib/Crypt/Hash.php @@ -1322,9 +1322,10 @@ class Hash list($lo, $hi) = $x; } + $mask = -1 ^ (-1 << $shift); return [ - ($hi << $shift) | (($lo >> (32 - $shift)) & (1 << $shift) - 1), - ($lo << $shift) | (($hi >> (32 - $shift)) & (1 << $shift) - 1) + ($hi << $shift) | (($lo >> (32 - $shift)) & $mask), + ($lo << $shift) | (($hi >> (32 - $shift)) & $mask) ]; } @@ -1481,7 +1482,8 @@ class Hash */ private static function rotateLeft64($x, $shift) { - return ($x << $shift) | (($x >> (64 - $shift)) & ((1 << $shift) - 1)); + $mask = -1 ^ (-1 << $shift); + return ($x << $shift) | (($x >> (64 - $shift)) & $mask); } /**