PublicKey   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 56
ccs 9
cts 9
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getBytes() 0 3 1
A verifySignature() 0 9 2
A getPublicKey() 0 3 1
1
<?php
2
3
/**
4
 * Copyright (c) 2020 UMI
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
declare(strict_types=1);
26
27
namespace UmiTop\UmiCore\Key\Ed25519;
28
29
use Exception;
30
use UmiTop\UmiCore\Key\PublicKeyInterface;
31
use UmiTop\UmiCore\Util\Ed25519\Ed25519;
32
33
/**
34
 * Класс для работы с публичными ключами.
35
 * @package UmiTop\UmiCore\Key\Ed25519
36
 */
37
class PublicKey implements PublicKeyInterface
38
{
39
    /** @var int Длина публичного ключа в байтах. */
40
    public const LENGTH = 32;
41
42
    /** @var string Публичный ключ в бинарном виде. */
43
    private $bytes;
44
45
    /**
46
     * PublicKey constructor.
47
     * @param string $bytes Публичный ключ в бинарном виде, в формате libsodium.
48
     * @throws Exception
49
     */
50 11
    public function __construct(string $bytes)
51
    {
52 11
        if (strlen($bytes) !== self::LENGTH) {
53 1
            throw new Exception('public key size should be 32 bytes');
54
        }
55
56 10
        $this->bytes = $bytes;
57 10
    }
58
59
    /**
60
     * Публичный ключ в формате libsodium, длина 32 байта.
61
     * @return string
62
     */
63 7
    public function getBytes(): string
64
    {
65 7
        return $this->bytes;
66
    }
67
68
    /**
69
     * Публичный ключ.
70
     * @return PublicKeyInterface
71
     */
72 1
    public function getPublicKey(): PublicKeyInterface
73
    {
74 1
        return $this;
75
    }
76
77
    /**
78
     * Проверяет цифровую подпись.
79
     * @param string $signature Подпись в бинарном виде.
80
     * @param string $message Сообщение в бинарном виде.
81
     * @return bool
82
     * @codeCoverageIgnore
83
     */
84
    public function verifySignature(string $signature, string $message): bool
85
    {
86
        if (function_exists('sodium_crypto_sign_verify_detached') === true) {
87
            return sodium_crypto_sign_verify_detached($signature, $message, $this->bytes);
88
        }
89
90
        $ed25519 = new Ed25519();
91
92
        return $ed25519->verify($signature, $message, $this->bytes);
93
    }
94
}
95