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

aplus-framework / crypto / 8075159324

16 Jan 2024 08:05PM UTC coverage: 100.0%. Remained the same
8075159324

push

github

natanfelles
Update issues link

136 of 136 relevant lines covered (100.0%)

1.99 hits per line

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

100.0
/src/SecretBox.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 Exception;
13
use LengthException;
14
use SensitiveParameter;
15
use SodiumException;
16

17
/**
18
 * Class SecretBox.
19
 *
20
 * @package crypto
21
 */
22
class SecretBox
23
{
24
    protected string $key;
25
    protected string $nonce;
26

27
    /**
28
     * SecretBox constructor.
29
     *
30
     * @param string $key
31
     * @param string $nonce
32
     *
33
     * @see SecretBox::makeKey()
34
     * @see SecretBox::makeNonce()
35
     *
36
     * @throws LengthException if key or nonce has not the required length
37
     */
38
    public function __construct(
39
        #[SensitiveParameter]
40
        string $key,
41
        #[SensitiveParameter]
42
        string $nonce
43
    ) {
44
        $this->validatedLengths($key, $nonce);
3✔
45
        $this->key = $key;
1✔
46
        $this->nonce = $nonce;
1✔
47
    }
48

49
    /**
50
     * Validates key and nonce.
51
     *
52
     * @param string $key
53
     * @param string $nonce
54
     *
55
     * @throws LengthException if key or nonce has not the required length
56
     */
57
    protected function validatedLengths(
58
        #[SensitiveParameter]
59
        string $key,
60
        #[SensitiveParameter]
61
        string $nonce
62
    ) : void {
63
        $length = \mb_strlen($key, '8bit');
3✔
64
        if ($length !== \SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
3✔
65
            throw new LengthException(
1✔
66
                'SecretBox key has not the required length (32 bytes), '
1✔
67
                . $length . ' given'
1✔
68
            );
1✔
69
        }
70
        $length = \mb_strlen($nonce, '8bit');
2✔
71
        if ($length !== \SODIUM_CRYPTO_SECRETBOX_NONCEBYTES) {
2✔
72
            throw new LengthException(
1✔
73
                'SecretBox nonce has not the required length (24 bytes), '
1✔
74
                . $length . ' given'
1✔
75
            );
1✔
76
        }
77
    }
78

79
    /**
80
     * Encrypts a secret box message.
81
     *
82
     * @param string $message
83
     *
84
     * @throws SodiumException
85
     *
86
     * @return string
87
     */
88
    public function encrypt(#[SensitiveParameter] string $message) : string
89
    {
90
        return \sodium_crypto_secretbox($message, $this->nonce, $this->key);
1✔
91
    }
92

93
    /**
94
     * Decrypts a secret box message ciphertext.
95
     *
96
     * @param string $ciphertext
97
     *
98
     * @throws SodiumException
99
     *
100
     * @return false|string
101
     */
102
    public function decrypt(#[SensitiveParameter] string $ciphertext) : false | string
103
    {
104
        return \sodium_crypto_secretbox_open($ciphertext, $this->nonce, $this->key);
1✔
105
    }
106

107
    /**
108
     * Makes a secret box key.
109
     *
110
     * @return string
111
     */
112
    public static function makeKey() : string
113
    {
114
        return \sodium_crypto_secretbox_keygen();
3✔
115
    }
116

117
    /**
118
     * Makes a secret box nonce with the correct length.
119
     *
120
     * @throws Exception if fail to get random bytes
121
     *
122
     * @return string
123
     */
124
    public static function makeNonce() : string
125
    {
126
        return \random_bytes(\SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
3✔
127
    }
128
}
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