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
1818
1919class 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