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

codeigniter4 / CodeIgniter4 / 37986190232

09 Oct 2026 08:19PM UTC coverage: 50.703% (-39.5%) from 90.183%
37986190232

Pull #10606

github

web-flow
Merge 2cebb1efa into 7822cde17
Pull Request #10606: fix: refresh REQUEST after URI GET normalization

17 of 30 new or added lines in 2 files covered. (56.67%)

10012 existing lines in 288 files now uncovered.

12871 of 25385 relevant lines covered (50.7%)

87.05 hits per line

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

2.86
/system/Encryption/Encryption.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;
15

16
use CodeIgniter\Encryption\Exceptions\EncryptionException;
17
use Config\Encryption as EncryptionConfig;
18

19
/**
20
 * CodeIgniter Encryption Manager
21
 *
22
 * Provides two-way keyed encryption via PHP's Sodium and/or OpenSSL extensions.
23
 * This class determines the driver, cipher, and mode to use, and then
24
 * initializes the appropriate encryption handler.
25
 *
26
 * @property-read string       $digest
27
 * @property-read string       $driver
28
 * @property-read list<string> $drivers
29
 * @property-read string       $key
30
 *
31
 * @see \CodeIgniter\Encryption\EncryptionTest
32
 */
33
class Encryption
34
{
35
    /**
36
     * The encrypter we create
37
     *
38
     * @var EncrypterInterface
39
     */
40
    protected $encrypter;
41

42
    /**
43
     * The driver being used
44
     *
45
     * @var string
46
     */
47
    protected $driver;
48

49
    /**
50
     * The key/seed being used
51
     *
52
     * @var string
53
     */
54
    protected $key;
55

56
    /**
57
     * The derived HMAC key
58
     *
59
     * @var string
60
     */
61
    protected $hmacKey;
62

63
    /**
64
     * HMAC digest to use
65
     *
66
     * @var string
67
     */
68
    protected $digest = 'SHA512';
69

70
    /**
71
     * Map of drivers to handler classes, in preference order
72
     *
73
     * @var list<string>
74
     */
75
    protected $drivers = [
76
        'OpenSSL',
77
        'Sodium',
78
    ];
79

80
    /**
81
     * Handlers that are to be installed
82
     *
83
     * @var array<string, bool>
84
     */
85
    protected $handlers = [];
86

87
    /**
88
     * @throws EncryptionException
89
     */
90
    public function __construct(?EncryptionConfig $config = null)
91
    {
UNCOV
92
        $config ??= new EncryptionConfig();
×
93

UNCOV
94
        $this->key    = $config->key;
×
UNCOV
95
        $this->driver = $config->driver;
×
UNCOV
96
        $this->digest = $config->digest;
×
97

UNCOV
98
        $this->handlers = [
×
UNCOV
99
            'OpenSSL' => extension_loaded('openssl'),
×
100
            // the SodiumHandler uses some API (like sodium_pad) that is available only on v1.0.14+
UNCOV
101
            'Sodium' => extension_loaded('sodium') && version_compare(SODIUM_LIBRARY_VERSION, '1.0.14', '>='),
×
UNCOV
102
        ];
×
103

UNCOV
104
        if (! in_array($this->driver, $this->drivers, true) || (array_key_exists($this->driver, $this->handlers) && ! $this->handlers[$this->driver])) {
×
UNCOV
105
            throw EncryptionException::forNoHandlerAvailable($this->driver);
×
106
        }
107
    }
108

109
    /**
110
     * Initialize or re-initialize an encrypter
111
     *
112
     * @return EncrypterInterface
113
     *
114
     * @throws EncryptionException
115
     */
116
    public function initialize(?EncryptionConfig $config = null)
117
    {
UNCOV
118
        if ($config instanceof EncryptionConfig) {
×
UNCOV
119
            $this->key    = $config->key;
×
UNCOV
120
            $this->driver = $config->driver;
×
UNCOV
121
            $this->digest = $config->digest;
×
122
        }
123

UNCOV
124
        if ($this->driver === null || $this->driver === '') {
×
UNCOV
125
            throw EncryptionException::forNoDriverRequested();
×
126
        }
127

UNCOV
128
        if (! in_array($this->driver, $this->drivers, true)) {
×
UNCOV
129
            throw EncryptionException::forUnKnownHandler($this->driver);
×
130
        }
131

UNCOV
132
        if (in_array($this->key, [null, '', '0'], true)) {
×
UNCOV
133
            throw EncryptionException::forNeedsStarterKey();
×
134
        }
135

UNCOV
136
        $this->hmacKey = bin2hex(\hash_hkdf($this->digest, $this->key));
×
137

UNCOV
138
        $handlerName     = 'CodeIgniter\\Encryption\\Handlers\\' . $this->driver . 'Handler';
×
UNCOV
139
        $this->encrypter = new $handlerName($config);
×
140

141
        // (array) '' is [''], not [], so the unset default must be filtered out here.
UNCOV
142
        $previousKeys = array_values(array_filter(
×
UNCOV
143
            (array) ($config->previousKeys ?? []),
×
UNCOV
144
            static fn ($key): bool => $key !== '',
×
UNCOV
145
        ));
×
146

UNCOV
147
        if ($previousKeys !== []) {
×
UNCOV
148
            $this->encrypter = new KeyRotationDecorator($this->encrypter, $previousKeys);
×
149
        }
150

UNCOV
151
        return $this->encrypter;
×
152
    }
153

154
    /**
155
     * Create a random key
156
     *
157
     * @param int $length Output length
158
     *
159
     * @return string
160
     */
161
    public static function createKey($length = 32)
162
    {
163
        return random_bytes($length);
8 ✔
164
    }
165

166
    /**
167
     * __get() magic, providing readonly access to some of our protected properties
168
     *
169
     * @param string $key Property name
170
     *
171
     * @return list<string>|string|null
172
     */
173
    public function __get($key)
174
    {
UNCOV
175
        if ($this->__isset($key)) {
×
UNCOV
176
            return $this->{$key};
×
177
        }
178

UNCOV
179
        return null;
×
180
    }
181

182
    /**
183
     * __isset() magic, providing checking for some of our protected properties
184
     *
185
     * @param string $key Property name
186
     */
187
    public function __isset($key): bool
188
    {
UNCOV
189
        return in_array($key, ['key', 'digest', 'driver', 'drivers'], true);
×
190
    }
191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc