DerPublicKeySerializer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A parse() 0 3 1
A __construct() 0 6 3
A getUncompressedKey() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Serializer\PublicKey;
5
6
use Mdanter\Ecc\Crypto\Key\PublicKeyInterface;
7
use Mdanter\Ecc\Math\GmpMathInterface;
8
use Mdanter\Ecc\Math\MathAdapterFactory;
9
use Mdanter\Ecc\Serializer\Point\PointSerializerInterface;
10
use Mdanter\Ecc\Serializer\Point\UncompressedPointSerializer;
11
use Mdanter\Ecc\Serializer\PublicKey\Der\Formatter;
12
use Mdanter\Ecc\Serializer\PublicKey\Der\Parser;
13
14
/**
15
 *
16
 * @link https://tools.ietf.org/html/rfc5480#page-3
17
 * @todo: review for full spec, should we support all prefixes here?
18
 */
19
class DerPublicKeySerializer implements PublicKeySerializerInterface
20
{
21
22
    const X509_ECDSA_OID = '1.2.840.10045.2.1';
23
24
    /**
25
     *
26
     * @var GmpMathInterface
27
     */
28
    private $adapter;
29
30
    /**
31
     *
32
     * @var Formatter
33
     */
34
    private $formatter;
35
36
    /**
37
     *
38
     * @var Parser
39
     */
40
    private $parser;
41
42
    /**
43
     * @param GmpMathInterface|null $adapter
44
     * @param PointSerializerInterface|null $pointSerializer
45
     */
46
    public function __construct(GmpMathInterface $adapter = null, PointSerializerInterface $pointSerializer = null)
47
    {
48
        $this->adapter = $adapter ?: MathAdapterFactory::getAdapter();
49
50
        $this->formatter = new Formatter();
51
        $this->parser = new Parser($this->adapter, $pointSerializer ?: new UncompressedPointSerializer());
52
    }
53
54
    /**
55
     *
56
     * @param  PublicKeyInterface $key
57
     * @return string
58
     */
59
    public function serialize(PublicKeyInterface $key): string
60
    {
61
        return $this->formatter->format($key);
62
    }
63
64
    /**
65
     * @param PublicKeyInterface $key
66
     * @return string
67
     */
68
    public function getUncompressedKey(PublicKeyInterface $key): string
69
    {
70
        return $this->formatter->encodePoint($key->getPoint());
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     * @see \Mdanter\Ecc\Serializer\PublicKey\PublicKeySerializerInterface::parse()
76
     */
77
    public function parse(string $string): PublicKeyInterface
78
    {
79
        return $this->parser->parse($string);
80
    }
81
}
82