blob: 7ed185f1b654980a1003c27bcae47d28ef087f80 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev7c251b32011-12-27 16:37:23 +02008 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
Andrey Andreev7c251b32011-12-27 16:37:23 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Encryption Class
42 *
Andrey Andreevfc4db342014-08-27 14:18:19 +030043 * Provides two-way keyed encoding using Mcrypt
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/encryption.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
51class CI_Encrypt {
52
Timothy Warren0688ac92012-04-20 10:25:04 -040053 /**
54 * Reference to the user's encryption key
55 *
56 * @var string
57 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030058 public $encryption_key = '';
Andrey Andreev56454792012-05-17 14:32:19 +030059
Timothy Warren0688ac92012-04-20 10:25:04 -040060 /**
61 * Type of hash operation
Andrey Andreev56454792012-05-17 14:32:19 +030062 *
Timothy Warren0688ac92012-04-20 10:25:04 -040063 * @var string
64 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030065 protected $_hash_type = 'sha1';
Andrey Andreev56454792012-05-17 14:32:19 +030066
Timothy Warren0688ac92012-04-20 10:25:04 -040067 /**
Calvin Tam55bc5052015-07-24 02:27:24 -070068 * Flag for the existence of mcrypt
Timothy Warren0688ac92012-04-20 10:25:04 -040069 *
70 * @var bool
71 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030072 protected $_mcrypt_exists = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030073
Timothy Warren0688ac92012-04-20 10:25:04 -040074 /**
75 * Current cipher to be used with mcrypt
76 *
77 * @var string
78 */
Greg Akerd1af1852011-12-25 21:59:30 -060079 protected $_mcrypt_cipher;
Andrey Andreev56454792012-05-17 14:32:19 +030080
Timothy Warren0688ac92012-04-20 10:25:04 -040081 /**
82 * Method for encrypting/decrypting data
83 *
84 * @var int
85 */
Greg Akerd1af1852011-12-25 21:59:30 -060086 protected $_mcrypt_mode;
Derek Allard2067d1a2008-11-13 22:59:24 +000087
Timothy Warren0688ac92012-04-20 10:25:04 -040088 /**
89 * Initialize Encryption class
Andrey Andreev56454792012-05-17 14:32:19 +030090 *
91 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040092 */
Greg Akera9263282010-11-10 15:26:43 -060093 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Andrey Andreev6eb77da2014-05-31 21:18:17 +030095 if (($this->_mcrypt_exists = function_exists('mcrypt_encrypt')) === FALSE)
96 {
97 show_error('The Encrypt library requires the Mcrypt extension.');
98 }
99
Andrey Andreev90726b82015-01-20 12:39:22 +0200100 log_message('info', 'Encrypt Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
102
103 // --------------------------------------------------------------------
104
105 /**
106 * Fetch the encryption key
107 *
108 * Returns it as MD5 in order to have an exact-length 128 bit key.
109 * Mcrypt is sensitive to keys that are not the correct length
110 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * @param string
112 * @return string
113 */
Greg Akerd1af1852011-12-25 21:59:30 -0600114 public function get_key($key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100116 if ($key === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100118 if ($this->encryption_key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
120 return $this->encryption_key;
121 }
122
Joffrey Jaffeuxba7f50b2012-06-06 01:40:01 +0200123 $key = config_item('encryption_key');
Derek Allard2067d1a2008-11-13 22:59:24 +0000124
Andrey Andreevf5652122017-01-19 15:17:00 +0200125 if ( ! self::strlen($key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
127 show_error('In order to use the encryption class requires that you set an encryption key in your config file.');
128 }
129 }
130
131 return md5($key);
132 }
133
134 // --------------------------------------------------------------------
135
136 /**
137 * Set the encryption key
138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500140 * @return CI_Encrypt
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 */
Greg Akerd1af1852011-12-25 21:59:30 -0600142 public function set_key($key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 $this->encryption_key = $key;
Greg Akerd1af1852011-12-25 21:59:30 -0600145 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Encode
152 *
153 * Encodes the message string using bitwise XOR encoding.
154 * The key is combined with a random hash, and then it
155 * too gets converted using XOR. The whole thing is then run
Andrey Andreev6eb77da2014-05-31 21:18:17 +0300156 * through mcrypt using the randomized key. The end result
157 * is a double-encrypted message string that is randomized
158 * with each call to this function, even if the supplied
159 * message and key are the same.
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 * @param string the string to encode
162 * @param string the key
163 * @return string
164 */
Greg Akerd1af1852011-12-25 21:59:30 -0600165 public function encode($string, $key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
Andrey Andreev6eb77da2014-05-31 21:18:17 +0300167 return base64_encode($this->mcrypt_encode($string, $this->get_key($key)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
170 // --------------------------------------------------------------------
171
172 /**
173 * Decode
174 *
175 * Reverses the above process
176 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 * @param string
178 * @param string
179 * @return string
180 */
Greg Akerd1af1852011-12-25 21:59:30 -0600181 public function decode($string, $key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Bogdan Lysenkoc16b4f42012-10-11 11:41:01 +0300183 if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string) OR base64_encode(base64_decode($string)) !== $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 {
185 return FALSE;
186 }
187
Andrey Andreev6eb77da2014-05-31 21:18:17 +0300188 return $this->mcrypt_decode(base64_decode($string), $this->get_key($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 }
190
191 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Jones09c77932010-08-31 13:17:10 -0500193 /**
194 * Encode from Legacy
195 *
196 * Takes an encoded string from the original Encryption class algorithms and
197 * returns a newly encoded string using the improved method added in 2.0.0
198 * This allows for backwards compatibility and a method to transition to the
199 * new encryption algorithms.
Barry Mienydd671972010-10-04 16:33:58 +0200200 *
Andrey Andreevbd202c92016-01-11 12:50:18 +0200201 * For more details, see https://codeigniter.com/user_guide/installation/upgrade_200.html#encryption
Derek Jones09c77932010-08-31 13:17:10 -0500202 *
Derek Jones09c77932010-08-31 13:17:10 -0500203 * @param string
204 * @param int (mcrypt mode constant)
205 * @param string
206 * @return string
207 */
Greg Akerd1af1852011-12-25 21:59:30 -0600208 public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key = '')
Derek Jones09c77932010-08-31 13:17:10 -0500209 {
Andrey Andreev6eb77da2014-05-31 21:18:17 +0300210 if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
Andrey Andreev7c251b32011-12-27 16:37:23 +0200211 {
212 return FALSE;
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Jones09c77932010-08-31 13:17:10 -0500215 // decode it first
216 // set mode temporarily to what it was when string was encoded with the legacy
Barry Mienydd671972010-10-04 16:33:58 +0200217 // algorithm - typically MCRYPT_MODE_ECB
Derek Jones09c77932010-08-31 13:17:10 -0500218 $current_mode = $this->_get_mode();
219 $this->set_mode($legacy_mode);
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Jones09c77932010-08-31 13:17:10 -0500221 $key = $this->get_key($key);
Derek Jones09c77932010-08-31 13:17:10 -0500222 $dec = base64_decode($string);
Derek Jones09c77932010-08-31 13:17:10 -0500223 if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
224 {
Andrey Andreevf696c1f2012-06-12 12:14:51 +0300225 $this->set_mode($current_mode);
Derek Jones09c77932010-08-31 13:17:10 -0500226 return FALSE;
227 }
228
229 $dec = $this->_xor_decode($dec, $key);
230
231 // set the mcrypt mode back to what it should be, typically MCRYPT_MODE_CBC
Derek Jones092103e2010-09-02 11:11:58 -0500232 $this->set_mode($current_mode);
Derek Jones09c77932010-08-31 13:17:10 -0500233
234 // and re-encode
235 return base64_encode($this->mcrypt_encode($dec, $key));
236 }
237
238 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 * XOR Decode
242 *
243 * Takes an encoded string and key as input and generates the
244 * plain-text original message
245 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 * @param string
247 * @param string
248 * @return string
249 */
Greg Akerd1af1852011-12-25 21:59:30 -0600250 protected function _xor_decode($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
252 $string = $this->_xor_merge($string, $key);
253
254 $dec = '';
Andrey Andreevf5652122017-01-19 15:17:00 +0200255 for ($i = 0, $l = self::strlen($string); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200257 $dec .= ($string[$i++] ^ $string[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
259
260 return $dec;
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * XOR key + string Combiner
267 *
268 * Takes a string and key as input and computes the difference using XOR
269 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param string
271 * @param string
272 * @return string
273 */
Greg Akerd1af1852011-12-25 21:59:30 -0600274 protected function _xor_merge($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 $hash = $this->hash($key);
277 $str = '';
Andrey Andreevf5652122017-01-19 15:17:00 +0200278
279 for ($i = 0, $ls = self::strlen($string), $lh = self::strlen($hash); $i < $ls; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200281 $str .= $string[$i] ^ $hash[($i % $lh)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
284 return $str;
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * Encrypt using Mcrypt
291 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 * @param string
293 * @param string
294 * @return string
295 */
Greg Akerd1af1852011-12-25 21:59:30 -0600296 public function mcrypt_encode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
Andrey Andreevf5652122017-01-19 15:17:00 +0200299 $init_vect = mcrypt_create_iv($init_size, MCRYPT_DEV_URANDOM);
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
301 }
302
303 // --------------------------------------------------------------------
304
305 /**
306 * Decrypt using Mcrypt
307 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 * @param string
309 * @param string
310 * @return string
311 */
Greg Akerd1af1852011-12-25 21:59:30 -0600312 public function mcrypt_decode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
314 $data = $this->_remove_cipher_noise($data, $key);
315 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
316
Andrey Andreevf5652122017-01-19 15:17:00 +0200317 if ($init_size > self::strlen($data))
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 return FALSE;
320 }
321
Andrey Andreevf5652122017-01-19 15:17:00 +0200322 $init_vect = self::substr($data, 0, $init_size);
323 $data = self::substr($data, $init_size);
324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
326 }
327
328 // --------------------------------------------------------------------
329
330 /**
331 * Adds permuted noise to the IV + encrypted data to protect
332 * against Man-in-the-middle attacks on CBC mode ciphers
333 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV
334 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 * @param string
336 * @param string
337 * @return string
338 */
Greg Akerd1af1852011-12-25 21:59:30 -0600339 protected function _add_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200341 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 $str = '';
343
Andrey Andreevf5652122017-01-19 15:17:00 +0200344 for ($i = 0, $j = 0, $ld = self::strlen($data), $lk = self::strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200346 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 $j = 0;
349 }
350
Andrey Andreev7c251b32011-12-27 16:37:23 +0200351 $str .= chr((ord($data[$i]) + ord($key[$j])) % 256);
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 }
353
354 return $str;
355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * Removes permuted noise from the IV + encrypted data, reversing
361 * _add_cipher_noise()
362 *
363 * Function description
364 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400365 * @param string $data
366 * @param string $key
Andrey Andreev38d0e932012-04-03 19:27:45 +0300367 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 */
Greg Akerd1af1852011-12-25 21:59:30 -0600369 protected function _remove_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200371 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 $str = '';
373
Andrey Andreevf5652122017-01-19 15:17:00 +0200374 for ($i = 0, $j = 0, $ld = self::strlen($data), $lk = self::strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200376 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $j = 0;
379 }
380
Andrey Andreev7c251b32011-12-27 16:37:23 +0200381 $temp = ord($data[$i]) - ord($key[$j]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382
383 if ($temp < 0)
384 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200385 $temp += 256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $str .= chr($temp);
389 }
390
391 return $str;
392 }
393
394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
397 * Set the Mcrypt Cipher
398 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300399 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500400 * @return CI_Encrypt
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 */
Greg Akerd1af1852011-12-25 21:59:30 -0600402 public function set_cipher($cipher)
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 $this->_mcrypt_cipher = $cipher;
Greg Akerd1af1852011-12-25 21:59:30 -0600405 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Set the Mcrypt Mode
412 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300413 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500414 * @return CI_Encrypt
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 */
Andrey Andreev7c251b32011-12-27 16:37:23 +0200416 public function set_mode($mode)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 $this->_mcrypt_mode = $mode;
Greg Akerd1af1852011-12-25 21:59:30 -0600419 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Get Mcrypt cipher Value
426 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300427 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 */
Greg Akerd1af1852011-12-25 21:59:30 -0600429 protected function _get_cipher()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300431 if ($this->_mcrypt_cipher === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200433 return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
435
436 return $this->_mcrypt_cipher;
437 }
438
439 // --------------------------------------------------------------------
440
441 /**
442 * Get Mcrypt Mode Value
443 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300444 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 */
Greg Akerd1af1852011-12-25 21:59:30 -0600446 protected function _get_mode()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300448 if ($this->_mcrypt_mode === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200450 return $this->_mcrypt_mode = MCRYPT_MODE_CBC;
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 return $this->_mcrypt_mode;
454 }
455
456 // --------------------------------------------------------------------
457
458 /**
459 * Set the Hash type
460 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * @param string
Andrey Andreevf4cb94e2012-01-19 15:16:55 +0200462 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 */
Greg Akerd1af1852011-12-25 21:59:30 -0600464 public function set_hash($type = 'sha1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Daniel Morrisada77752012-10-04 10:24:16 +0100466 $this->_hash_type = in_array($type, hash_algos()) ? $type : 'sha1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
468
469 // --------------------------------------------------------------------
470
471 /**
472 * Hash encode a string
473 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @param string
475 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200476 */
Greg Akerd1af1852011-12-25 21:59:30 -0600477 public function hash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
Daniel Morrisa9923f52012-10-03 19:37:09 +0100479 return hash($this->_hash_type, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 }
Andrey Andreev38d0e932012-04-03 19:27:45 +0300481
Andrey Andreevf5652122017-01-19 15:17:00 +0200482 // --------------------------------------------------------------------
483
484 /**
485 * Byte-safe strlen()
486 *
487 * @param string $str
488 * @return int
489 */
490 protected static function strlen($str)
491 {
492 return defined('MB_OVERLOAD_STRING')
493 ? mb_strlen($str, '8bit')
494 : strlen($str);
495 }
496
497 // --------------------------------------------------------------------
498
499 /**
500 * Byte-safe substr()
501 *
502 * @param string $str
503 * @param int $start
504 * @param int $length
505 * @return string
506 */
507 protected static function substr($str, $start, $length = NULL)
508 {
509 if (defined('MB_OVERLOAD_STRING'))
510 {
511 // mb_substr($str, $start, null, '8bit') returns an empty
512 // string on PHP 5.3
513 isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
514 return mb_substr($str, $start, $length, '8bit');
515 }
516
517 return isset($length)
518 ? substr($str, $start, $length)
519 : substr($str, $start);
520 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000521}