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

codeigniter4 / CodeIgniter4 / 8677009716

13 Apr 2024 11:45PM UTC coverage: 84.44% (-2.2%) from 86.607%
8677009716

push

github

web-flow
Merge pull request #8776 from kenjis/fix-findQualifiedNameFromPath-Cannot-declare-class-v3

fix: Cannot declare class CodeIgniter\Config\Services, because the name is already in use

0 of 3 new or added lines in 1 file covered. (0.0%)

478 existing lines in 72 files now uncovered.

20318 of 24062 relevant lines covered (84.44%)

188.23 hits per line

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

97.22
/system/Encryption/Handlers/SodiumHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Encryption\Handlers;
15

16
use CodeIgniter\Encryption\Exceptions\EncryptionException;
17

18
/**
19
 * SodiumHandler uses libsodium in encryption.
20
 *
21
 * @see https://github.com/jedisct1/libsodium/issues/392
22
 * @see \CodeIgniter\Encryption\Handlers\SodiumHandlerTest
23
 */
24
class SodiumHandler extends BaseHandler
25
{
26
    /**
27
     * Starter key
28
     *
29
     * @var string
30
     */
31
    protected $key = '';
32

33
    /**
34
     * Block size for padding message.
35
     *
36
     * @var int
37
     */
38
    protected $blockSize = 16;
39

40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function encrypt($data, $params = null)
44
    {
45
        $this->parseParams($params);
6✔
46

47
        if (empty($this->key)) {
6✔
48
            throw EncryptionException::forNeedsStarterKey();
1✔
49
        }
50

51
        // create a nonce for this operation
52
        $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); // 24 bytes
5✔
53

54
        // add padding before we encrypt the data
55
        if ($this->blockSize <= 0) {
5✔
56
            throw EncryptionException::forEncryptionFailed();
1✔
57
        }
58

59
        $data = sodium_pad($data, $this->blockSize);
4✔
60

61
        // encrypt message and combine with nonce
62
        $ciphertext = $nonce . sodium_crypto_secretbox($data, $nonce, $this->key);
4✔
63

64
        // cleanup buffers
65
        sodium_memzero($data);
4✔
66
        sodium_memzero($this->key);
4✔
67

68
        return $ciphertext;
4✔
69
    }
70

71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function decrypt($data, $params = null)
75
    {
76
        $this->parseParams($params);
4✔
77

78
        if (empty($this->key)) {
4✔
79
            throw EncryptionException::forNeedsStarterKey();
1✔
80
        }
81

82
        if (mb_strlen($data, '8bit') < (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES + SODIUM_CRYPTO_SECRETBOX_MACBYTES)) {
3✔
83
            // message was truncated
84
            throw EncryptionException::forAuthenticationFailed();
1✔
85
        }
86

87
        // Extract info from encrypted data
88
        $nonce      = self::substr($data, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
2✔
89
        $ciphertext = self::substr($data, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
2✔
90

91
        // decrypt data
92
        $data = sodium_crypto_secretbox_open($ciphertext, $nonce, $this->key);
2✔
93

94
        if ($data === false) {
2✔
95
            // message was tampered in transit
UNCOV
96
            throw EncryptionException::forAuthenticationFailed(); // @codeCoverageIgnore
×
97
        }
98

99
        // remove extra padding during encryption
100
        if ($this->blockSize <= 0) {
2✔
101
            throw EncryptionException::forAuthenticationFailed();
1✔
102
        }
103

104
        $data = sodium_unpad($data, $this->blockSize);
1✔
105

106
        // cleanup buffers
107
        sodium_memzero($ciphertext);
1✔
108
        sodium_memzero($this->key);
1✔
109

110
        return $data;
1✔
111
    }
112

113
    /**
114
     * Parse the $params before doing assignment.
115
     *
116
     * @param array|string|null $params
117
     *
118
     * @return void
119
     *
120
     * @throws EncryptionException If key is empty
121
     */
122
    protected function parseParams($params)
123
    {
124
        if ($params === null) {
6✔
125
            return;
5✔
126
        }
127

128
        if (is_array($params)) {
4✔
129
            if (isset($params['key'])) {
2✔
130
                $this->key = $params['key'];
2✔
131
            }
132

133
            if (isset($params['blockSize'])) {
2✔
134
                $this->blockSize = $params['blockSize'];
2✔
135
            }
136

137
            return;
2✔
138
        }
139

140
        $this->key = (string) $params;
2✔
141
    }
142
}
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