+++ /dev/null
-<?php
-
-class IPF_Crypto
-{
- public static function encrypt($string)
- {
- $string = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, self::get_key(), $string, MCRYPT_MODE_CBC, self::get_iv());
- $string = base64_encode($string);
- return $string;
- }
-
- public static function decrypt($string)
- {
- $string = base64_decode($string);
- $string = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, self::get_key(), $string, MCRYPT_MODE_CBC, self::get_iv());
- $string = rtrim($string, "\0");
- return $string;
- }
-
- public static function sign($string)
- {
- return hash_hmac('sha1', $string, self::get_key());
- }
-
- private static function get_key()
- {
- $secret = self::secretKey();
- return sha1($secret.$secret);
- }
-
- private static function get_iv()
- {
- $secret = self::secretKey();
- return sha1(sha1($secret));
- }
-
- private static function secretKey()
- {
- $secret_key = IPF::get('secret_key');
- if ($secret_key)
- return $secret_key;
- else
- throw new Exception('Security error: "secret_key" is not set in the configuration file.');
- }
-}
public function configure(Container $container)
{
+ $secret_key = $container['settings']->get('secret_key');
+ if (!$secret_key) {
+ throw new Exception('Security error: "secret_key" is not set in the configuration file.');
+ }
+
$this->backends = [
- new CookieSessionBackend(),
- new DBSessionBackend($container, $container['settings']->get('secret_key')),
+ new CookieSessionBackend($secret_key),
+ new DBSessionBackend($container, $secret_key),
];
}
class CookieSessionBackend implements SessionBackend
{
+ /** @var string */
+ private $secret_key;
+
+ function __construct($secret_key)
+ {
+ $this->secret_key = $secret_key;
+ }
+
public function getData($key)
{
$key = explode('|', $key, 2);
if (count($key) !== 2)
return null;
list($data, $sign) = $key;
- if (IPF_Crypto::sign($data) !== $sign)
+ if ($this->sign($data) !== $sign)
return null;
return unserialize(base64_decode($data));
public function save($key, $data)
{
$encoded = base64_encode(serialize($data));
- return $encoded . '|' . IPF_Crypto::sign($encoded);
+ return $encoded . '|' . $this->sign($encoded);
}
public function delete($key)
{
// DO NOTHING
}
+
+ private function sign($data)
+ {
+ $key = sha1($this->secret_key . $this->secret_key);
+ return hash_hmac('sha1', $data, $key);
+ }
}
class DBSessionBackend implements SessionBackend