Keypair   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 2
b 0
f 0
dl 0
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSodiumKeypair() 0 5 1
A getPublicKey() 0 3 1
A getSecretKey() 0 3 1
A __construct() 0 13 3
1
<?php declare(strict_types=1);
2
3
namespace ncryptf;
4
5
use InvalidArgumentException;
6
7
final class Keypair
8
{
9
    /**
10
     * Secret key
11
     *
12
     * @var string
13
     */
14
    private $secretKey;
15
16
    /**
17
     * Public Key
18
     *
19
     * @var string
20
     */
21
    private $publicKey;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param string $secret
27
     * @param string $public
28
     */
29
    public function __construct(string $secretKey, string $publicKey)
30
    {
31
        if (\strlen($secretKey) % 16 !== 0) {
32
            throw new InvalidArgumentException(sprintf("Secret key should be a multiple of %d bytes.", 16));
33
        }
34
35
        $this->secretKey = $secretKey;
36
37
        if (\strlen($publicKey) % 4 !== 0) {
38
            throw new InvalidArgumentException(sprintf("Public key should be a multiple of %d bytes.", 4));
39
        }
40
41
        $this->publicKey = $publicKey;
42
    }
43
44
    /**
45
     * Returns the public key
46
     *
47
     * @return string|null
48
     */
49
    public function getPublicKey() :? string
50
    {
51
        return $this->publicKey;
52
    }
53
54
    /**
55
     * Returns the secret key
56
     *
57
     * @return string|null
58
     */
59
    public function getSecretKey() :? string
60
    {
61
        return $this->secretKey;
62
    }
63
64
    /**
65
     * Returns the sodium keypair
66
     *
67
     * @return string
68
     */
69
    public function getSodiumKeypair()
70
    {
71
        return \sodium_crypto_box_keypair_from_secretkey_and_publickey(
72
            $this->getSecretKey(),
73
            $this->getPublicKey()
74
        );
75
    }
76
}
77