Skip to content

Commit f1283f5

Browse files
committed
Add HMAC & encoding to OpenSSLHandler
1 parent 2ed08d1 commit f1283f5

8 files changed

Lines changed: 248 additions & 639 deletions

File tree

application/Config/Encryption.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Encryption extends BaseConfig
5151
|
5252
| Use HMAC message authentication (true/false)
5353
*/
54-
public $hmac = true;
54+
public $hmac = 'HMAC';
5555

5656
/*
5757
|--------------------------------------------------------------------------
@@ -69,6 +69,6 @@ class Encryption extends BaseConfig
6969
|
7070
| If true, base64 encode results, and expect base64-encoded ciphertext.
7171
*/
72-
public $base64 = true;
72+
public $base64 = 'base64';
7373

7474
}

public/Checks.php

Lines changed: 3 additions & 292 deletions
Original file line numberDiff line numberDiff line change
@@ -1,292 +1,3 @@
1-
<?php namespace App\Controllers;
2-
3-
use CodeIgniter\API\ResponseTrait;
4-
use CodeIgniter\Config\Services;
5-
use CodeIgniter\Controller;
6-
use CodeIgniter\I18n\Time;
7-
use CodeIgniter\Model;
8-
use Config\Database;
9-
use Tests\Support\Models\JobModel;
10-
11-
class Checks extends Controller
12-
{
13-
use ResponseTrait;
14-
15-
public function index()
16-
{
17-
session()->start();
18-
}
19-
20-
21-
public function escape()
22-
{
23-
$db = Database::connect();
24-
$db->initialize();
25-
26-
$jobs = $db->table('job')
27-
->whereNotIn('name', ['Politician', 'Accountant'])
28-
->get()
29-
->getResult();
30-
31-
die(var_dump($jobs));
32-
}
33-
34-
public function password()
35-
{
36-
$db = Database::connect();
37-
$db->initialize();
38-
39-
$result = $db->table('misc')
40-
->insert([
41-
'key' => 'password',
42-
'value' => '$2y$10$ErQlCj/Mo10il.FthAm0WOjYdf3chZEGPFqaPzjqOX2aj2uYf5Ihq'
43-
]);
44-
45-
die(var_dump($result));
46-
}
47-
48-
49-
public function forms()
50-
{
51-
helper('form');
52-
53-
var_dump(form_open());
54-
}
55-
56-
public function api()
57-
{
58-
$data = array(
59-
"total_users" => 3,
60-
"users" => array(
61-
array(
62-
"id" => 1,
63-
"name" => "Nitya",
64-
"address" => array(
65-
"country" => "India",
66-
"city" => "Kolkata",
67-
"zip" => 700102,
68-
)
69-
),
70-
array(
71-
"id" => 2,
72-
"name" => "John",
73-
"address" => array(
74-
"country" => "USA",
75-
"city" => "Newyork",
76-
"zip" => "NY1234",
77-
)
78-
),
79-
array(
80-
"id" => 3,
81-
"name" => "Viktor",
82-
"address" => array(
83-
"country" => "Australia",
84-
"city" => "Sydney",
85-
"zip" => 123456,
86-
)
87-
),
88-
)
89-
);
90-
91-
return $this->respond($data);
92-
}
93-
94-
public function db()
95-
{
96-
$db = Database::connect();
97-
$db->initialize();
98-
99-
$query = $db->prepare(function($db){
100-
return $db->table('user')->insert([
101-
'name' => 'a',
102-
'email' => 'b@example.com',
103-
'country' => 'x'
104-
]);
105-
});
106-
107-
$query->execute('foo', 'foo@example.com', 'US');
108-
}
109-
110-
public function format()
111-
{
112-
echo '<pre>';
113-
var_dump($this->response->getHeaderLine('content-type'));
114-
}
115-
116-
public function model()
117-
{
118-
$model = new class() extends Model {
119-
protected $table = 'job';
120-
};
121-
122-
$results = $model->findAll();
123-
124-
$developer = $model->findWhere('name', 'Developer');
125-
126-
$politician = $model->find(3);
127-
128-
}
129-
130-
public function curl()
131-
{
132-
$client = Services::curlrequest([
133-
'debug' => true,
134-
'follow_redirects' => true,
135-
'json' => ['foo' => 'bar']
136-
]);
137-
138-
echo '<pre>';
139-
$response = $client->request('PUT', 'http://ci4.dev/checks/catch');
140-
echo $response->getBody();
141-
}
142-
143-
// Simply echos back what's given in the body.
144-
public function catch()
145-
{
146-
$body = print_r($this->request->getRawInput(), true);
147-
echo $body;
148-
}
149-
150-
public function redirect()
151-
{
152-
redirect('/checks/model');
153-
}
154-
155-
public function image()
156-
{
157-
$info = Services::image('imagick')
158-
->withFile("/Users/kilishan/Documents/BobHeader.jpg")
159-
->getFile()
160-
->getProperties(true);
161-
162-
dd(ENVIRONMENT);
163-
164-
$images = Services::image('imagick')
165-
->getVersion();
166-
// ->withFile("/Users/kilishan/Documents/BobHeader.jpg")
167-
// ->resize(500, 100, true)
168-
// ->crop(200, 75, 20, 0, false)
169-
// ->rotate(90)
170-
// ->save('/Users/kilishan/temp.jpg');
171-
172-
// $images = Services::image('imagick')
173-
// ->withFile("/Users/kilishan/Documents/BobHeader.jpg")
174-
// ->fit(500, 100, 'bottom-left')
175-
// ->text('Bob is Back!', [
176-
// 'fontPath' => '/Users/kilishan/Downloads/Calibri.ttf',
177-
// 'fontSize' => 40,
178-
// 'padding' => 0,
179-
// 'opacity' => 0.5,
180-
// 'vAlign' => 'top',
181-
// 'hAlign' => 'right',
182-
// 'withShadow' => true,
183-
// ])
184-
// ->save('/Users/kilishan/temp.jpg', 100);
185-
186-
187-
ddd($images);
188-
}
189-
190-
public function time()
191-
{
192-
$time = new Time();
193-
194-
echo($time);
195-
echo '<br/>';
196-
echo Time::now();
197-
echo '<br/>';
198-
echo Time::parse('First Monday of December');
199-
echo '<br/>';
200-
201-
$time = new Time('Next Monday');
202-
die($time);
203-
}
204-
205-
public function csp()
206-
{
207-
// $this->response->CSP->reportOnly(true);
208-
$this->response->CSP->setDefaultSrc(base_url());
209-
$this->response->CSP->addStyleSrc('unsafe-inline');
210-
$this->response->CSP->addStyleSrc('https://maxcdn.bootstrapcdn.com');
211-
212-
echo <<<EOF
213-
<!doctype html>
214-
<html>
215-
<head>
216-
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
217-
</head>
218-
<body>
219-
<style {csp-style-nonce}>
220-
body { background: #efefef; }
221-
</style>
222-
223-
</body>
224-
</html>
225-
EOF;
226-
227-
}
228-
229-
public function upload()
230-
{
231-
if ($this->request->getMethod() == 'post')
232-
{
233-
$this->validate([
234-
'avatar' => 'uploaded[avatar]|ext_in[avatar,png,jpg,jpeg,gif]'
235-
]);
236-
237-
/**
238-
* @var \CodeIgniter\HTTP\Files\UploadedFile
239-
*/
240-
$file = $this->request->getFile('avatar');
241-
242-
echo "Name: {$file->getName()}<br/>";
243-
echo "Temp Name: {$file->getTempName()}<br/>";
244-
echo "Original Name: {$file->getClientName()}<br/>";
245-
echo "Random Name: {$file->getRandomName()}<br/>";
246-
echo "Extension: {$file->getExtension()}<br/>";
247-
echo "Client Extension: {$file->getClientExtension()}<br/>";
248-
echo "Guessed Extension: {$file->guessExtension()}<br/>";
249-
echo "MimeType: {$file->getMimeType()}<br/>";
250-
echo "IsValid: {$file->isValid()}<br/>";
251-
echo "Size (b): {$file->getSize()}<br/>";
252-
echo "Size (kb): {$file->getSize('kb')}<br/>";
253-
echo "Size (mb): {$file->getSize('mb')}<br/>";
254-
echo "Size (mb): {$file->getSize('mb')}<br/>";
255-
echo "Path: {$file->getPath()}<br/>";
256-
echo "RealPath: {$file->getRealPath()}<br/>";
257-
echo "Filename: {$file->getFilename()}<br/>";
258-
echo "Basename: {$file->getBasename()}<br/>";
259-
echo "Pathname: {$file->getPathname()}<br/>";
260-
echo "Permissions: {$file->getPerms()}<br/>";
261-
echo "Inode: {$file->getInode()}<br/>";
262-
echo "Owner: {$file->getOwner()}<br/>";
263-
echo "Group: {$file->getGroup()}<br/>";
264-
echo "ATime: {$file->getATime()}<br/>";
265-
echo "MTime: {$file->getMTime()}<br/>";
266-
echo "CTime: {$file->getCTime()}<br/>";
267-
268-
dd($file);
269-
}
270-
271-
echo <<<EOF
272-
<!doctype html>
273-
<html>
274-
<body>
275-
276-
<form action="" method="post" enctype="multipart/form-data">
277-
278-
<input type="file" name="avatar">
279-
280-
<input type="submit" value="Upload">
281-
282-
</form>
283-
284-
</body>
285-
</html>
286-
287-
EOF;
288-
;
289-
}
290-
291-
292-
}
1+
<?php
2+
echo 'Hi there ...<br/>';
3+
echo extension_loaded('openssl');

system/Encryption/Encryption.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ class Encryption
8383
'driver' => 'OpenSSL', // The PHP extension we plan to use
8484
'key' => '', // no starting key material
8585
'cipher' => 'AES-256-CBC', // Encryption cipher
86-
'hmac' => true, // Use HMAC message authentication (true/false)
86+
'hmac' => 'HMAC', // Use HMAC message authentication (true/false)
8787
'digest' => 'SHA512', // HMAC digest algorithm to use
88-
'base64' => true, // Base64 encoding?
88+
'base64' => 'base64', // Base64 encoding?
8989
];
9090
protected $driver, $key, $cipher, $hmac, $digest, $base64;
9191

@@ -152,7 +152,7 @@ public function __construct($params = [])
152152
*
153153
* @throws \CodeIgniter\Encryption\EncryptionException
154154
*/
155-
public function initialize(array $params = null)
155+
public function initialize(array $params = [])
156156
{
157157
$params = $this->properParams($params);
158158

@@ -172,6 +172,9 @@ public function initialize(array $params = null)
172172
if ( ! isset($this->digests[$params['digest']]))
173173
throw new EncryptionException("Unknown digest '" . $params['digest'] . "' specified.");
174174

175+
// Derive a secret key for the encrypter
176+
$params['secret'] = bin2hex(hash_hkdf($this->digest, $params['key']));
177+
175178
$handlerName = 'CodeIgniter\\Encryption\\Handlers\\' . $this->driver . 'Handler';
176179
$this->encrypter = new $handlerName($params);
177180
return $this->encrypter;

0 commit comments

Comments
 (0)