-
Notifications
You must be signed in to change notification settings - Fork 238
/
Encrypt.php
205 lines (174 loc) · 5 KB
/
Encrypt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\storage\session\strategy;
use lithium\core\ConfigException;
/**
* This strategy allows you to encrypt your `Session` and / or `Cookie` data so that it
* is not stored in cleartext on the client side.
*
* Example configuration:
*
* {{{
* Session::config(array('default' => array(
* 'adapter' => 'Cookie',
* 'strategies' => array('Encrypt' => array('secret' => 'foobar'))
* )));
* }}}
*
* By default, this strategy uses the AES algorithm in the CBC mode. You can override this
* defaults by passing a different `cipher` and/or `mode` to the config like this:
*
* {{{
* Session::config(array('default' => array(
* 'adapter' => 'Cookie',
* 'strategies' => array('Encrypt' => array(
* 'cipher' => MCRYPT_RIJNDAEL_128,
* 'mode' => MCRYPT_MODE_ECB,
* 'secret' => 'foobar'
* ))
* )));
* }}}
*
* @link http://www.php.net/manual/en/mcrypt.ciphers.php List of supported ciphers.
* @link http://www.php.net/manual/en/mcrypt.constants.php List of supported modes.
*/
class Encrypt extends \lithium\core\Object {
/**
* Holds the initialization vector.
*/
protected static $_vector = null;
/**
* Constructor.
*
* @param array $config Configuration array. You can override the default cipher and mode.
*/
public function __construct(array $config = array()) {
if (!isset($config['secret'])) {
throw new ConfigException("Encrypt strategy requires a secret key.");
}
$defaults = array(
'cipher' => MCRYPT_RIJNDAEL_256,
'mode' => MCRYPT_MODE_CBC
);
parent::__construct($config + $defaults);
extract($this->_config);
$this->_config['vector'] = static::_vector($cipher, $mode);
}
/**
* Read encryption method.
*
* @param
* @param
* @return
*/
public function read($data, array $options = array()) {
$class = $options['class'];
$encrypted = $class::read(null, array('strategies' => false));
$key = isset($options['key']) ? $options['key'] : null;
if (!isset($encrypted['__encrypted']) || !$encrypted['__encrypted']) {
return isset($encrypted[$key]) ? $encrypted[$key] : null;
}
$current = $this->_decrypt($encrypted['__encrypted']);
if ($key) {
return isset($current[$key]) ? $current[$key] : null;
} else {
return $current;
}
}
/**
* Write encryption method.
*
* @param
* @param
* @return
*/
public function write($data, array $options = array()) {
$class = $options['class'];
$futureData = $this->read(null, array('key' => null) + $options) ?: array();
$futureData = array($options['key'] => $data) + $futureData;
$payload = empty($futureData) ? null : $this->_encrypt($futureData);
$class::write('__encrypted', $payload, array('strategies' => false) + $options);
return $data;
}
/**
* Delete encryption method.
*
* @param
* @param
* @return
*/
public function delete($data, array $options = array()) {
$class = $options['class'];
$futureData = $this->read(null, array('key' => null) + $options) ?: array();
unset($futureData[$options['key']]);
$payload = empty($futureData) ? null : $this->_encrypt($futureData);
$class::write('__encrypted', $payload, array('strategies' => false) + $options);
return $data;
}
/**
* Determines if the Mcrypt extension has been installed.
*
* @return boolean `true` if enabled, `false` otherwise
*/
public static function enabled() {
return extension_loaded('mcrypt');
}
/**
* Serialize and encrypt a given data array.
*
* @param
* @return
*/
protected function _encrypt($decrypted = array()) {
extract($this->_config);
$encrypted = mcrypt_encrypt($cipher, $secret, serialize($decrypted), $mode, $vector);
$data = base64_encode($encrypted) . base64_encode($vector);
return $data;
}
/**
* Decrypt and unserialize a previously encrypted string.
*
* @param
* @return
*/
protected function _decrypt($encrypted) {
extract($this->_config);
$vectorSize = strlen(base64_encode(str_repeat(" ", static::_vectorSize($cipher, $mode))));
$vector = base64_decode(substr($encrypted, -$vectorSize));
$data = base64_decode(substr($encrypted, 0, -$vectorSize));
$decrypted = mcrypt_decrypt($cipher, $secret, $data, $mode, $vector);
$data = unserialize(trim($decrypted));
return $data;
}
/**
* Generates an initialization vector.
*
* @param
* @param
* @return string Returns an initialization vector.
* @link http://www.php.net/manual/en/function.mcrypt-create-iv.php
*/
protected static function _vector($cipher, $mode) {
if (static::$_vector) {
return static::$_vector;
}
$size = static::_vectorSize($cipher, $mode);
return static::$_vector = mcrypt_create_iv($size, MCRYPT_DEV_URANDOM);
}
/**
* Returns the vector size vor a given cipher and mode.
*
* @param
* @param
* @return
*/
protected static function _vectorSize($cipher, $mode) {
return mcrypt_get_iv_size($cipher, $mode);
}
}
?>