1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | |||||
4 | namespace Mdanter\Ecc\Serializer\Point; |
||||
5 | |||||
6 | use Mdanter\Ecc\Primitives\PointInterface; |
||||
7 | use Mdanter\Ecc\Primitives\CurveFpInterface; |
||||
8 | use Mdanter\Ecc\Serializer\Util\CurveOidMapper; |
||||
9 | use Mdanter\Ecc\Util\BinaryString; |
||||
10 | |||||
11 | class UncompressedPointSerializer implements PointSerializerInterface |
||||
12 | { |
||||
13 | /** |
||||
14 | * @param PointInterface $point |
||||
15 | * @return string |
||||
16 | */ |
||||
17 | public function serialize(PointInterface $point): string |
||||
18 | { |
||||
19 | $length = CurveOidMapper::getByteSize($point->getCurve()) * 2; |
||||
20 | |||||
21 | $hexString = '04'; |
||||
22 | $hexString .= str_pad(gmp_strval($point->getX(), 16), $length, '0', STR_PAD_LEFT); |
||||
23 | $hexString .= str_pad(gmp_strval($point->getY(), 16), $length, '0', STR_PAD_LEFT); |
||||
24 | |||||
25 | return $hexString; |
||||
26 | } |
||||
27 | |||||
28 | /** |
||||
29 | * @param CurveFpInterface $curve |
||||
30 | * @param string $data |
||||
31 | * @return PointInterface |
||||
32 | */ |
||||
33 | public function unserialize(CurveFpInterface $curve, string $data): PointInterface |
||||
34 | { |
||||
35 | if (BinaryString::substring($data, 0, 2) != '04') { |
||||
36 | throw new \InvalidArgumentException('Invalid data: only uncompressed keys are supported.'); |
||||
37 | } |
||||
38 | |||||
39 | $data = BinaryString::substring($data, 2); |
||||
40 | $dataLength = BinaryString::length($data); |
||||
41 | |||||
42 | $x = gmp_init(BinaryString::substring($data, 0, $dataLength / 2), 16); |
||||
43 | $y = gmp_init(BinaryString::substring($data, $dataLength / 2), 16); |
||||
44 | |||||
45 | return $curve->getPoint($x, $y); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
$y can also be of type resource ; however, parameter $y of Mdanter\Ecc\Primitives\C...FpInterface::getPoint() does only seem to accept GMP , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
46 | } |
||||
47 | } |
||||
48 |