From 0f95ddc692d350f367e3b0501ba1e56a5c7de4b8 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 2 Sep 2012 01:13:21 -0500 Subject: [PATCH] Updated fix for php.pear.net bug # 18015 toBits() on an object for which setPrecision(8) has been called should always return 8 bits. As such we'll just iterate backwards instead of forward. --- phpseclib/Math/BigInteger.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index d0d66e53..df6d96a2 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -605,11 +605,11 @@ class Math_BigInteger { { $hex = $this->toHex($twos_compliment); $bits = ''; - for ($i = 0, $end = strlen($hex) & 0xFFFFFFF8; $i < $end; $i+=8) { - $bits.= str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT); + for ($i = strlen($hex) - 8, $start = strlen($hex) & 7; $i >= $start; $i-=8) { + $bits = str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT) . $bits; } - if ($end != strlen($hex)) { // hexdec('') == 0 - $bits.= str_pad(decbin(hexdec(substr($hex, $end))), strlen($hex) & 7, '0', STR_PAD_LEFT); + if ($start) { // hexdec('') == 0 + $bits = str_pad(decbin(hexdec(substr($hex, 0, $start))), 8, '0', STR_PAD_LEFT) . $bits; } return $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0'); }