@@ -554,14 +554,37 @@ */ static protected function bigNumToBin($bn) { - if (extension_loaded('gmp')) { - $s = gmp_strval($bn, 16); - if (strlen($s) % 2 != 0) { - $s = '0' . $s; - } - return pack("H*", $s); - } else if (extension_loaded('bcmath')) { - $cmp = bccomp($bn, 0); + if (extension_loaded('gmp')) { + $cmp = gmp_cmp($bn, 0); + if ($cmp < 0) { + throw new Zend_OpenId_Exception( + 'Big integer arithmetic error', + Zend_OpenId_Exception::ERROR_LONG_MATH); + } + + if ($cmp == 0) { + return "\x00"; + } + + $bytes = array(); + + while (gmp_cmp($bn, 0) > 0) { + array_unshift($bytes, gmp_mod($bn, 256)); + $bn = gmp_div($bn, pow(2, 8)); + } + + if ($bytes && ($bytes[0] > 127)) { + array_unshift($bytes, 0); + } + + $string = ''; + foreach ($bytes as $byte) { + $string .= pack('C', $byte); + } + + return $string; + } else if (extension_loaded('bcmath')) { + $cmp = bccomp($bn, 0); if ($cmp == 0) { return (chr(0)); } else if ($cmp < 0) {