Skip to content

Commit 382adc0

Browse files
author
Chris Carlevato
committed
Add improved hash support, phpunit 6 support
1 parent 6e7da1d commit 382adc0

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

_examples/complete.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
'path' => '/',
1212
'domain' => 'localhost',
1313
'secure' => false,
14-
'hash' => 1,
14+
'hash' => 'sha512',
1515
'decoy' => true,
1616
'min' => 5,
1717
'max' => 10
@@ -26,6 +26,7 @@
2626
$session->setValue('random', (string)rand(0, 5000));
2727
$session->setValue('fixed','some value');
2828
$session->setValue('array', array('one thing', 'two thing', 'has_key' => 'red thing', 'blue_thing' => array('text', false, 50)));
29+
$session->setValue('hashed', 'this is my string', true);
2930

3031
// Session Variable Set Outside of Class
3132
$_SESSION['Outside'] = 'a session value set the old fashioned way';
@@ -37,6 +38,7 @@
3738
$random = $session->getValue('random');
3839
$fixed = $session->getValue('fixed');
3940
$array = print_r($session->getValue('array'), true);
41+
$hashed = $session->getValue('hashed');
4042

4143

4244
// Output Session Variables
@@ -59,6 +61,9 @@
5961
</p>
6062
<p>
6163
<b>array:</b> {$array}
64+
</p>
65+
<p>
66+
<b>hashed:</b> {$hashed}
6267
</p>
6368
<p>
6469
<b>Outside:</b> {$_SESSION['Outside']}

_tests/SessionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace ChristopherL;
1919

20-
class SessionTest extends \PHPUnit_Framework_TestCase
20+
class SessionTest extends \PHPUnit\Framework\TestCase
2121
{
2222
/**
2323
* @runInSeparateProcess
@@ -216,7 +216,7 @@ public function testSessionHashValue()
216216
$session->setValue('my_hashed_variable', 'plain text value', true);
217217

218218
// Verifiy creation of hashed session value
219-
$this->assertEquals($session->getValue('my_hashed_variable'), sha1('plain text value'));
219+
$this->assertEquals($session->getValue('my_hashed_variable'), hash($session->getHash(), 'plain text value'));
220220
}
221221

222222
/**

cl_session.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* @copyright 2015- Chris Carlevato (https://github.com/chrislarrycarl)
3+
* @copyright 2015-2017 Chris Carlevato (https://github.com/chrislarrycarl)
44
* @license http://www.gnu.org/licenses/lgpl-2.1.html
55
*
66
* This library is free software; you can redistribute it and/or
@@ -18,7 +18,7 @@
1818

1919
class Session
2020
{
21-
const VERSION = '0.2';
21+
const VERSION = '0.3';
2222

2323
protected $name, $domain, $hash, $key, $path, $secure, $decoy, $min_time, $max_time;
2424
protected $failmsg = 'Session generation failed.';
@@ -29,7 +29,7 @@ class Session
2929
* - path: Server path the cookie is available on (Default: /)
3030
* - domain: Domain the cookie is available to (Default: localhost)
3131
* - secure: Only transmit the cookie over https (Default: false)
32-
* - hash: 0 = MD5(128 bits), 1 = SHA1(160 bits) (Default: 1)
32+
* - hash: 0 = MD5, 1 = SHA1, or supported hash name (Default: 1)
3333
* - decoy: True/False to generate fake PHPSESSID cookie (Default: true)
3434
* - min: Min time, in seconds, to regenerate session (Default: 60)
3535
* - max: Max time, in seconds, to regenerate session (Default: 600).
@@ -38,7 +38,6 @@ class Session
3838
*/
3939
public function __construct($config = array())
4040
{
41-
4241
// Create session settings based on provided config
4342
$settings = array(
4443
'name' => isset($config['name']) ? $config['name'] : 'clsession',
@@ -160,19 +159,32 @@ protected function getSecure()
160159
/**
161160
* Set cookie id hash method.
162161
*
163-
* @param int $hash 0 = MD5, 1 = SHA1 (Default: 1)
162+
* @param int/string $hash 0 = MD5, 1 = SHA1, or supported hash name (Default: 1)
164163
*/
165164
protected function setHash($hash = 1)
166165
{
166+
if ($hash === 0) {
167+
$hash = 'md5';
168+
}
169+
else if ($hash === 1) {
170+
$hash = 'sha256';
171+
}
172+
else if (in_array($hash, hash_algos())) {
173+
$hash = $hash;
174+
}
175+
else {
176+
$this->Error('Invalid hash algorithm selected.');
177+
}
178+
167179
$this->hash = $hash;
168180
}
169181

170182
/**
171-
* Get cookie id hash setting.
183+
* Get session hash setting.
172184
*
173185
* @return int Cookie Hash Setting
174186
*/
175-
protected function getHash()
187+
public function getHash()
176188
{
177189
return $this->hash;
178190
}
@@ -316,7 +328,7 @@ public function setValue($key, $value, $hash = false)
316328
{
317329
// if requested, hash the value before saving it
318330
if ($hash) {
319-
$value = sha1($value);
331+
$value = hash($this->getHash(), $value);
320332
}
321333

322334
$_SESSION['clValues'][$key] = $value;

0 commit comments

Comments
 (0)