diff --git a/phpseclib/Math/BigInteger.php b/phpseclib/Math/BigInteger.php index 9365d5cf..adf50838 100644 --- a/phpseclib/Math/BigInteger.php +++ b/phpseclib/Math/BigInteger.php @@ -40,7 +40,7 @@ use phpseclib3\Math\BigInteger\Engines\Engine; * @author Jim Wigginton * @access public */ -class BigInteger +class BigInteger implements \JsonSerializable { /** * Main Engine @@ -438,6 +438,20 @@ class BigInteger } } + /** + * JSON Serialize + * + * Will be called, automatically, when json_encode() is called on a BigInteger object. + */ + public function jsonSerialize() + { + $result = ['hex' => $this->toHex(true)]; + if ($this->precision > 0) { + $result['precision'] = $this->getPrecision(); + } + return $result; + } + /** * Performs modular exponentiation. * diff --git a/phpseclib/Math/BigInteger/Engines/Engine.php b/phpseclib/Math/BigInteger/Engines/Engine.php index 6c152202..fe6bff80 100644 --- a/phpseclib/Math/BigInteger/Engines/Engine.php +++ b/phpseclib/Math/BigInteger/Engines/Engine.php @@ -28,7 +28,7 @@ use phpseclib3\Math\BigInteger; * @author Jim Wigginton * @access public */ -abstract class Engine +abstract class Engine implements \JsonSerializable { /* final protected */ const PRIMES = [ 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, @@ -371,6 +371,20 @@ abstract class Engine } } + /** + * JSON Serialize + * + * Will be called, automatically, when json_encode() is called on a BigInteger object. + */ + public function jsonSerialize() + { + $result = ['hex' => $this->toHex(true)]; + if ($this->precision > 0) { + $result['precision'] = $this->precision; + } + return $result; + } + /** * Converts a BigInteger to a base-10 number. * diff --git a/phpseclib/Math/Common/FiniteField/Integer.php b/phpseclib/Math/Common/FiniteField/Integer.php index ab968e26..0291ebd6 100644 --- a/phpseclib/Math/Common/FiniteField/Integer.php +++ b/phpseclib/Math/Common/FiniteField/Integer.php @@ -21,6 +21,18 @@ namespace phpseclib3\Math\Common\FiniteField; * @author Jim Wigginton * @access public */ -abstract class Integer +abstract class Integer implements \JsonSerializable { + /** + * JSON Serialize + * + * Will be called, automatically, when json_encode() is called on a BigInteger object. + * + * PHP Serialize isn't supported because unserializing would require the factory be + * serialized as well and that just sounds like too much + */ + public function jsonSerialize() + { + return ['hex' => $this->toHex(true)]; + } }