Skip to content

Commit ba74877

Browse files
committed
openssl encrypt/decryption strict arg refactor
1 parent 96aed2c commit ba74877

4 files changed

Lines changed: 47 additions & 32 deletions

File tree

src/Session.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class Session
5252
private static $ssl_enabled = true;
5353

5454
public static $write = false;
55+
56+
public static $id = '';
5557

5658
private static function init()
5759
{
@@ -131,33 +133,34 @@ private static function init()
131133
*
132134
* @param string $id
133135
*/
134-
public static function id(string $id)
136+
public static function id(string $id = ''): string
135137
{
136138
if (empty(self::$initialized))
137139
{
138140
self::init();
139141
}
140142

141-
if (self::$started)
142-
{
143-
throw new \RuntimeException('Session is active. The session id must be set before Session::start().');
144-
}
145-
elseif (strlen($id) > 250)
146-
{
147-
throw new \RuntimeException('Session id cant be above 250 characters long');
148-
}
149-
elseif (headers_sent($filename, $line_num))
143+
if ($id != '')
150144
{
151-
throw new \RuntimeException(sprintf('ID must be set before any output is sent to the browser (file: %s, line: %s)', $filename, $line_num));
152-
}
153-
elseif (preg_match('/^[\w-,]{1,128}$/', $id) < 1)
154-
{
155-
throw new \InvalidArgumentException('Invalid Session ID');
156-
}
157-
else
158-
{
159-
session_id($id);
145+
if (self::$started)
146+
{
147+
throw new \RuntimeException('Session is active. The session id must be set before Session::start().');
148+
}
149+
elseif (headers_sent($filename, $line_num))
150+
{
151+
throw new \RuntimeException(sprintf('ID must be set before any output is sent to the browser (file: %s, line: %s)', $filename, $line_num));
152+
}
153+
elseif (preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $id) < 1)
154+
{
155+
throw new \InvalidArgumentException('Invalid Session ID.');
156+
}
157+
else
158+
{
159+
session_id($id);
160+
}
160161
}
162+
163+
return self::$id;
161164
}
162165

163166

@@ -222,18 +225,18 @@ public static function decrypt(string $data): string
222225
$ct = substr($data, 16);
223226

224227
$rounds = 3; // depends on key length
225-
$data00 = $password.$salt;
226-
$hash = array();
228+
$data00 = $password . $salt;
229+
$hash = [];
227230
$hash[0] = hash('sha256', $data00, true);
228231
$result = $hash[0];
229232
for ($i = 1; $i < $rounds; $i++)
230233
{
231-
$hash[$i] = hash('sha256', $hash[$i - 1].$data00, true);
234+
$hash[$i] = hash('sha256', $hash[$i - 1] . $data00, true);
232235
$result .= $hash[$i];
233236
}
234237
$key = substr($result, 0, 32);
235238
$iv = substr($result, 32,16);
236-
$decrypted = openssl_decrypt($ct, 'AES-256-CBC', $key, true, $iv);
239+
$decrypted = openssl_decrypt($ct, 'AES-256-CBC', $key, 1, $iv);
237240

238241
return ( ! $decrypted) ? '' : $decrypted;
239242
}
@@ -259,14 +262,14 @@ public static function encrypt(string $data): string
259262
// Salt the key(32) and iv(16) = 48
260263
while (strlen($salted) < 48)
261264
{
262-
$dx = hash('sha256', $dx.$password.$salt, true);
265+
$dx = hash('sha256', $dx . $password . $salt, true);
263266
$salted .= $dx;
264267
}
265268

266269
$key = substr($salted, 0, 32);
267270
$iv = substr($salted, 32,16);
268271

269-
$encrypted_data = openssl_encrypt($data, 'AES-256-CBC', $key, true, $iv);
272+
$encrypted_data = openssl_encrypt($data, 'AES-256-CBC', $key, 1, $iv);
270273
return base64_encode($salt . $encrypted_data);
271274
}
272275
}

src/Session/Save.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ private function init()
138138
session_start();
139139

140140
# store current session ID
141-
$this->config['sess_id'] = session_id();
141+
$id = session_id();
142+
$this->config['sess_id'] = $id;
143+
Session::$id = $id;
142144

143145
$_ = session_get_cookie_params();
144146
setcookie($this->config['name'], $this->config['sess_id'], $_['lifetime'], $_['path'], $_['domain'], $_['secure'], $_['httponly']);
@@ -306,7 +308,9 @@ public function rotate(bool $delete_old = true, bool $write_nd_close = true)
306308

307309
session_start();
308310
session_regenerate_id($delete_old);
309-
$this->config['sess_id'] = session_id();
311+
$id = session_id();
312+
$this->config['sess_id'] = $id;
313+
Session::$id = $id;
310314
if ($write_nd_close)
311315
{
312316
session_write_close();
@@ -340,7 +344,6 @@ public function clear(string $segment = '')
340344
}
341345
}
342346

343-
unset($this->config['session'][$this->config['namespace']]);
344347
$this->commit();
345348
}
346349

@@ -349,10 +352,18 @@ public function clear(string $segment = '')
349352
* @param bool $in_flash
350353
* @return bool
351354
*/
352-
public function exists(string $name, bool $in_flash = false): bool
355+
public function exist(string $name, string $segment = null, bool $in_flash = false): bool
353356
{
354357
$namespace = $this->config['namespace'];
355-
$segment = $this->config['segment'];
358+
359+
if ($segment != null)
360+
{
361+
$segment = 'segment:' . $segment;
362+
}
363+
else
364+
{
365+
$segment = $this->config['segment'];
366+
}
356367
return isset($this->config['session'][$namespace][$segment][$in_flash ? 'flash' : 'set'][$name]);
357368
}
358369

src/Session/config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
*/
3939

4040
return [
41-
'driver' => 'file', # Name of session driver to use: [file|pdo|cookie|redis|memcached]
41+
'driver' => 'cookie', # Name of session driver to use: [file|pdo|cookie|redis|memcached]
4242
'name' => '_Bittr_SESSID', # session name
4343
'cache_limiter' => 'none', # http://php.net/manual/en/function.session-cache-limiter.php
4444

4545
#[security]
46-
'encrypt_data' => false, # Allow encryption of session data.
46+
'encrypt_data' => true, # Allow encryption of session data.
4747
'key' => 'secret_salt_key', # Encryption key. ineffective if 'encrypt_data' = false
4848
'match_ip' => false, # If set to true, IP address will be stored and validated on each I/O.
4949
'match_browser' => false, # If set to true, browser will be stored and validated on each I/O.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__GLOBAL|a:1:{s:14:"segment:static";a:2:{s:3:"set";a:1:{s:4:"name";s:5:"chrys";}s:5:"flash";a:0:{}}}

0 commit comments

Comments
 (0)