1
0
mirror of https://github.com/danog/phpseclib.git synced 2024-12-03 18:18:05 +01:00

make it so BigIntegers can be JSON serialized

This commit is contained in:
terrafrost 2022-03-10 20:26:46 -06:00
parent 815aa23b39
commit 26d8f7a250
3 changed files with 43 additions and 3 deletions

View File

@ -40,7 +40,7 @@ use phpseclib3\Math\BigInteger\Engines\Engine;
* @author Jim Wigginton <terrafrost@php.net>
* @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.
*

View File

@ -28,7 +28,7 @@ use phpseclib3\Math\BigInteger;
* @author Jim Wigginton <terrafrost@php.net>
* @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.
*

View File

@ -21,6 +21,18 @@ namespace phpseclib3\Math\Common\FiniteField;
* @author Jim Wigginton <terrafrost@php.net>
* @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)];
}
}