• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / crypto / 11228560566

23 Aug 2024 08:22PM UTC coverage: 100.0%. Remained the same
11228560566

push

github

natanfelles
Upgrade coding standard

132 of 132 relevant lines covered (100.0%)

1.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Box.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Crypto Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Crypto;
11

12
use LengthException;
13
use LogicException;
14
use SensitiveParameter;
15
use SodiumException;
16

17
/**
18
 * Class Box.
19
 *
20
 * @package crypto
21
 */
22
class Box
23
{
24
    use BoxTrait;
25

26
    protected string $secretKey;
27
    protected string $publicKey;
28
    protected ?string $nonce;
29

30
    /**
31
     * Box constructor.
32
     *
33
     * @param string $secretKey
34
     * @param string $publicKey
35
     * @param string|null $nonce
36
     *
37
     * @see BoxTrait::makePublicKey()
38
     * @see BoxTrait::makeSecretKey()
39
     * @see BoxTrait::makeNonce()
40
     *
41
     * @throws LengthException if nonce is set has not the required length
42
     */
43
    public function __construct(
44
        #[SensitiveParameter]
45
        string $secretKey,
46
        #[SensitiveParameter]
47
        string $publicKey,
48
        #[SensitiveParameter]
49
        ?string $nonce = null
50
    ) {
51
        $this->secretKey = $secretKey;
3✔
52
        $this->publicKey = $publicKey;
3✔
53
        if ($nonce !== null) {
3✔
54
            $this->validateNonce($nonce);
2✔
55
        }
56
        $this->nonce = $nonce;
3✔
57
    }
58

59
    /**
60
     * Validates a nonce.
61
     *
62
     * @param string $nonce
63
     *
64
     * @throws LengthException if nonce has not the required length
65
     */
66
    protected function validateNonce(#[SensitiveParameter] string $nonce) : void
67
    {
68
        $length = \mb_strlen($nonce, '8bit');
3✔
69
        if ($length !== \SODIUM_CRYPTO_BOX_NONCEBYTES) {
3✔
70
            throw new LengthException(
1✔
71
                'Box nonce has not the required length (24 bytes), '
1✔
72
                . $length . ' given'
1✔
73
            );
1✔
74
        }
75
    }
76

77
    /**
78
     * @param string|null $nonce
79
     *
80
     * @throws LengthException if nonce is set and has not the required length
81
     * @throws LogicException if nonce param is null and nonce was not set in
82
     * constructor
83
     *
84
     * @return string
85
     */
86
    protected function getNonce(#[SensitiveParameter] ?string $nonce) : string
87
    {
88
        if ($nonce !== null) {
2✔
89
            $this->validateNonce($nonce);
1✔
90
            return $nonce;
1✔
91
        }
92
        if ($this->nonce === null) {
2✔
93
            throw new LogicException('Nonce was not set');
1✔
94
        }
95
        return $this->nonce;
2✔
96
    }
97

98
    /**
99
     * Gets the keypair from the secret and public keys.
100
     *
101
     * @throws SodiumException
102
     *
103
     * @return string
104
     */
105
    protected function getKeyPair() : string
106
    {
107
        return \sodium_crypto_box_keypair_from_secretkey_and_publickey(
1✔
108
            $this->secretKey,
1✔
109
            $this->publicKey
1✔
110
        );
1✔
111
    }
112

113
    /**
114
     * Encrypts a box message.
115
     *
116
     * @param string $message
117
     * @param string|null $nonce The message nonce or null to use the nonce set
118
     * int the constructor
119
     *
120
     * @throws LengthException if nonce is set and has not the required length
121
     * @throws LogicException if nonce param is null and nonce was not set in
122
     * the constructor
123
     * @throws SodiumException
124
     *
125
     * @return string
126
     */
127
    public function encrypt(
128
        #[SensitiveParameter]
129
        string $message,
130
        #[SensitiveParameter]
131
        ?string $nonce = null
132
    ) : string {
133
        return \sodium_crypto_box(
1✔
134
            $message,
1✔
135
            $this->getNonce($nonce),
1✔
136
            $this->getKeyPair()
1✔
137
        );
1✔
138
    }
139

140
    /**
141
     * Decrypts a box message ciphertext.
142
     *
143
     * @param string $ciphertext
144
     * @param string|null $nonce The message nonce or null to use the nonce set
145
     * int the constructor
146
     *
147
     * @throws LengthException if nonce is set and has not the required length
148
     * @throws LogicException if nonce param is null and nonce was not set in
149
     * the constructor
150
     * @throws SodiumException
151
     *
152
     * @return false|string
153
     */
154
    public function decrypt(
155
        #[SensitiveParameter]
156
        string $ciphertext,
157
        #[SensitiveParameter]
158
        ?string $nonce = null
159
    ) : false | string {
160
        return \sodium_crypto_box_open(
1✔
161
            $ciphertext,
1✔
162
            $this->getNonce($nonce),
1✔
163
            $this->getKeyPair()
1✔
164
        );
1✔
165
    }
166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc