Skip to content

Commit 69cff58

Browse files
authored
Merge pull request #3307 from michalsn/encryption_key
Add hex2bin prefix handling for encryption key
2 parents 924a0b9 + fff1510 commit 69cff58

9 files changed

Lines changed: 90 additions & 3 deletions

File tree

env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@
8383
# contentsecuritypolicy.sandbox = false
8484
# contentsecuritypolicy.upgradeInsecureRequests = false
8585

86+
#--------------------------------------------------------------------
87+
# ENCRYPTION
88+
#--------------------------------------------------------------------
89+
90+
# encryption.key =
91+
# encryption.driver = OpenSSL
92+
8693
#--------------------------------------------------------------------
8794
# HONEYPOT
8895
#--------------------------------------------------------------------

system/Config/BaseConfig.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ public function __construct()
9292
foreach ($properties as $property)
9393
{
9494
$this->initEnvValue($this->$property, $property, $prefix, $shortPrefix);
95+
96+
// Handle hex2bin prefix
97+
if ($shortPrefix === 'encryption' && $property === 'key' && strpos($this->$property, 'hex2bin:') === 0)
98+
{
99+
$this->$property = hex2bin(substr($this->$property, 8));
100+
}
95101
}
96102

97103
if (defined('ENVIRONMENT') && ENVIRONMENT !== 'testing')

system/Config/DotEnv.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __construct(string $path, string $file = '.env')
7777
public function load(): bool
7878
{
7979
$vars = $this->parse();
80-
80+
8181
return ($vars === null ? false : true);
8282
}
8383

@@ -182,6 +182,12 @@ public function normaliseVariable(string $name, string $value = ''): array
182182

183183
$value = $this->resolveNestedVariables($value);
184184

185+
// Handle hex2bin prefix
186+
if ($name === 'encryption.key' && strpos($value, 'hex2bin:') === 0)
187+
{
188+
$value = hex2bin(substr($value, 8));
189+
}
190+
185191
return [
186192
$name,
187193
$value,

tests/system/Config/BaseConfigTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ protected function setUp(): void
2222
{
2323
require $this->fixturesFolder . '/RegistrarConfig.php';
2424
}
25+
if (! class_exists('Encryption', false))
26+
{
27+
require $this->fixturesFolder . '/Encryption.php';
28+
}
2529
}
2630

2731
//--------------------------------------------------------------------
@@ -144,6 +148,36 @@ public function testSetsDefaultValues()
144148

145149
//--------------------------------------------------------------------
146150

151+
/**
152+
* @runInSeparateProcess
153+
* @preserveGlobalState disabled
154+
*/
155+
public function testSetsDefaultValuesEncryption()
156+
{
157+
$dotenv = new DotEnv($this->fixturesFolder, 'encryption.env');
158+
$dotenv->load();
159+
$config = new \Encryption();
160+
161+
// override config with ENV var
162+
$this->assertEquals('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', bin2hex($config->key));
163+
$this->assertEquals('OpenSSL', $config->driver);
164+
}
165+
166+
//--------------------------------------------------------------------
167+
168+
public function testSetsDefaultValuesHex2Bin()
169+
{
170+
$dotenv = new DotEnv($this->fixturesFolder, 'commented.env');
171+
$dotenv->load();
172+
$config = new \Encryption();
173+
174+
// override config with ENV var
175+
$this->assertEquals('84cf2c0811d5daf9e1c897825a3debce91f9a33391e639f72f7a4740b30675a2', bin2hex($config->key));
176+
$this->assertEquals('MCrypt', $config->driver);
177+
}
178+
179+
//--------------------------------------------------------------------
180+
147181
public function testRecognizesLooseValues()
148182
{
149183
$dotenv = new DotEnv($this->fixturesFolder, 'loose.env');

tests/system/Config/DotEnvTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ public function testLoadsVars()
5656

5757
//--------------------------------------------------------------------
5858

59+
public function testLoadsHex2Bin()
60+
{
61+
$dotenv = new DotEnv($this->fixturesFolder, 'encryption.env');
62+
$dotenv->load();
63+
64+
$value = getenv('encryption.key');
65+
66+
$this->assertTrue(! empty($value));
67+
$this->assertEquals('f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', bin2hex($value));
68+
$this->assertEquals('hex2bin:f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6', getenv('different.key'));
69+
$this->assertEquals('OpenSSL', getenv('encryption.driver'));
70+
}
71+
72+
//--------------------------------------------------------------------
73+
5974
public function testLoadsNoneStringFiles()
6075
{
6176
$dotenv = new DotEnv($this->fixturesFolder, 2);

tests/system/Config/fixtures/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ SimpleConfig.crew.pilot = Wash
2626
SimpleConfig.crew.comms = true
2727
SimpleConfig.crew.doctor = false
2828

29+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
class Encryption extends \CodeIgniter\Config\BaseConfig
4+
{
5+
public $key = 'hex2bin:84cf2c0811d5daf9e1c897825a3debce91f9a33391e639f72f7a4740b30675a2';
6+
7+
public $driver = 'MCrypt';
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
encryption.key=hex2bin:f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6
2+
encryption.driver=OpenSSL
3+
different.key=hex2bin:f699c7fd18a8e082d0228932f3acd40e1ef5ef92efcedda32842a211d62f0aa6

user_guide_src/source/libraries/encryption.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,16 @@ a more friendly manner. For example::
128128
// Get a hex-encoded representation of the key:
129129
$encoded = bin2hex(Encryption::createKey(32));
130130

131-
// Put the same value in your config with hex2bin(),
131+
// Put the same value with hex2bin(),
132132
// so that it is still passed as binary to the library:
133-
$key = hex2bin(<your hex-encoded key>);
133+
$key = hex2bin('your-hex-encoded-key');
134+
135+
// In the Encryption config class you can use a special 'hex2bin:'
136+
// prefix so that the value is still passed as binary to the library:
137+
public $key = 'hex2bin:your-hex-encoded-key';
138+
139+
// You can also use the same prefix in your .env file
140+
encryption.key = hex2bin:your-hex-encoded-key
134141

135142
You might find the same technique useful for the results
136143
of encryption::

0 commit comments

Comments
 (0)