PrivateKey::getSecret()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Mdanter\Ecc\Crypto\Key;
5
6
/**
7
 * *********************************************************************
8
 * Copyright (C) 2012 Matyas Danter
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining
11
 * a copy of this software and associated documentation files (the "Software"),
12
 * to deal in the Software without restriction, including without limitation
13
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
 * and/or sell copies of the Software, and to permit persons to whom the
15
 * Software is furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included
18
 * in all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
24
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26
 * OTHER DEALINGS IN THE SOFTWARE.
27
 * ***********************************************************************
28
 */
29
30
use Mdanter\Ecc\Crypto\EcDH\EcDH;
31
use Mdanter\Ecc\Crypto\EcDH\EcDHInterface;
32
use Mdanter\Ecc\Math\GmpMathInterface;
33
use Mdanter\Ecc\Primitives\CurveFpInterface;
34
use Mdanter\Ecc\Primitives\GeneratorPoint;
35
use Mdanter\Ecc\Primitives\PointInterface;
36
37
/**
38
 * This class serves as public - private key exchange for signature verification.
39
 */
40
class PrivateKey implements PrivateKeyInterface
41
{
42
    /**
43
     * @var GeneratorPoint
44
     */
45
    private $generator;
46
47
    /**
48
     * @var \GMP
49
     */
50
    private $secretMultiplier;
51
52
    /**
53
     * @var GmpMathInterface
54
     */
55
    private $adapter;
56
57
    /**
58
     * @param GmpMathInterface $adapter
59
     * @param GeneratorPoint $generator
60
     * @param \GMP $secretMultiplier
61
     */
62
    public function __construct(GmpMathInterface $adapter, GeneratorPoint $generator, \GMP $secretMultiplier)
63
    {
64
        $this->adapter = $adapter;
65
        $this->generator = $generator;
66
        $this->secretMultiplier = $secretMultiplier;
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     * @see \Mdanter\Ecc\Crypto\Key\PrivateKeyInterface::getPublicKey()
72
     */
73
    public function getPublicKey(): PublicKeyInterface
74
    {
75
        return new PublicKey($this->adapter, $this->generator, $this->generator->mul($this->secretMultiplier));
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     * @see \Mdanter\Ecc\Crypto\Key\PrivateKeyInterface::getPoint()
81
     */
82
    public function getPoint(): GeneratorPoint
83
    {
84
        return $this->generator;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     * @see \Mdanter\Ecc\Crypto\Key\PrivateKeyInterface::getCurve()
90
     */
91
    public function getCurve(): CurveFpInterface
92
    {
93
        return $this->generator->getCurve();
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     * @see \Mdanter\Ecc\Crypto\Key\PrivateKeyInterface::getSecret()
99
     */
100
    public function getSecret(): \GMP
101
    {
102
        return $this->secretMultiplier;
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     * @see \Mdanter\Ecc\Crypto\Key\PrivateKeyInterface::createExchange()
108
     */
109
    public function createExchange(PublicKeyInterface $recipient = null): EcDHInterface
110
    {
111
        $ecdh = new EcDH($this->adapter);
112
        $ecdh
113
            ->setSenderKey($this)
114
            ->setRecipientKey($recipient);
115
116
        return $ecdh;
117
    }
118
}
119