blob: e54ce4950a868a09899920c73989881af01d77e0 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev7c251b32011-12-27 16:37:23 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7c251b32011-12-27 16:37:23 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Encryption Class
31 *
32 * Provides two-way keyed encoding using XOR Hashing and Mcrypt
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/encryption.html
39 */
40class CI_Encrypt {
41
Timothy Warren0688ac92012-04-20 10:25:04 -040042 /**
43 * Reference to the user's encryption key
44 *
45 * @var string
46 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030047 public $encryption_key = '';
Andrey Andreev56454792012-05-17 14:32:19 +030048
Timothy Warren0688ac92012-04-20 10:25:04 -040049 /**
50 * Type of hash operation
Andrey Andreev56454792012-05-17 14:32:19 +030051 *
Timothy Warren0688ac92012-04-20 10:25:04 -040052 * @var string
53 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030054 protected $_hash_type = 'sha1';
Andrey Andreev56454792012-05-17 14:32:19 +030055
Timothy Warren0688ac92012-04-20 10:25:04 -040056 /**
57 * Flag for the existance of mcrypt
58 *
59 * @var bool
60 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030061 protected $_mcrypt_exists = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030062
Timothy Warren0688ac92012-04-20 10:25:04 -040063 /**
64 * Current cipher to be used with mcrypt
65 *
66 * @var string
67 */
Greg Akerd1af1852011-12-25 21:59:30 -060068 protected $_mcrypt_cipher;
Andrey Andreev56454792012-05-17 14:32:19 +030069
Timothy Warren0688ac92012-04-20 10:25:04 -040070 /**
71 * Method for encrypting/decrypting data
72 *
73 * @var int
74 */
Greg Akerd1af1852011-12-25 21:59:30 -060075 protected $_mcrypt_mode;
Derek Allard2067d1a2008-11-13 22:59:24 +000076
Timothy Warren0688ac92012-04-20 10:25:04 -040077 /**
78 * Initialize Encryption class
Andrey Andreev56454792012-05-17 14:32:19 +030079 *
80 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040081 */
Greg Akera9263282010-11-10 15:26:43 -060082 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000083 {
Andrey Andreev38d0e932012-04-03 19:27:45 +030084 $this->_mcrypt_exists = function_exists('mcrypt_encrypt');
Andrey Andreevcc6dbda2012-01-08 06:35:17 +020085 log_message('debug', 'Encrypt Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
87
88 // --------------------------------------------------------------------
89
90 /**
91 * Fetch the encryption key
92 *
93 * Returns it as MD5 in order to have an exact-length 128 bit key.
94 * Mcrypt is sensitive to keys that are not the correct length
95 *
Derek Allard2067d1a2008-11-13 22:59:24 +000096 * @param string
97 * @return string
98 */
Greg Akerd1af1852011-12-25 21:59:30 -060099 public function get_key($key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100101 if ($key === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100103 if ($this->encryption_key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
105 return $this->encryption_key;
106 }
107
Joffrey Jaffeuxba7f50b2012-06-06 01:40:01 +0200108 $key = config_item('encryption_key');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109
Greg Akerd1af1852011-12-25 21:59:30 -0600110 if ($key === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 {
112 show_error('In order to use the encryption class requires that you set an encryption key in your config file.');
113 }
114 }
115
116 return md5($key);
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Set the encryption key
123 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 * @param string
Andrew Podner4296a652012-12-17 07:51:15 -0500125 * @return CI_Encrypt
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 */
Greg Akerd1af1852011-12-25 21:59:30 -0600127 public function set_key($key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
129 $this->encryption_key = $key;
Greg Akerd1af1852011-12-25 21:59:30 -0600130 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Encode
137 *
138 * Encodes the message string using bitwise XOR encoding.
139 * The key is combined with a random hash, and then it
140 * too gets converted using XOR. The whole thing is then run
141 * through mcrypt (if supported) using the randomized key.
142 * The end result is a double-encrypted message string
143 * that is randomized with each call to this function,
144 * even if the supplied message and key are the same.
145 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 * @param string the string to encode
147 * @param string the key
148 * @return string
149 */
Greg Akerd1af1852011-12-25 21:59:30 -0600150 public function encode($string, $key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200152 $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_encode' : '_xor_encode';
153 return base64_encode($this->$method($string, $this->get_key($key)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Decode
160 *
161 * Reverses the above process
162 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 * @param string
164 * @param string
165 * @return string
166 */
Greg Akerd1af1852011-12-25 21:59:30 -0600167 public function decode($string, $key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Bogdan Lysenkoc16b4f42012-10-11 11:41:01 +0300169 if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string) OR base64_encode(base64_decode($string)) !== $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 return FALSE;
172 }
173
Andrey Andreev7c251b32011-12-27 16:37:23 +0200174 $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_decode' : '_xor_decode';
175 return $this->$method(base64_decode($string), $this->get_key($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 }
177
178 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Jones09c77932010-08-31 13:17:10 -0500180 /**
181 * Encode from Legacy
182 *
183 * Takes an encoded string from the original Encryption class algorithms and
184 * returns a newly encoded string using the improved method added in 2.0.0
185 * This allows for backwards compatibility and a method to transition to the
186 * new encryption algorithms.
Barry Mienydd671972010-10-04 16:33:58 +0200187 *
Derek Jones09c77932010-08-31 13:17:10 -0500188 * For more details, see http://codeigniter.com/user_guide/installation/upgrade_200.html#encryption
189 *
Derek Jones09c77932010-08-31 13:17:10 -0500190 * @param string
191 * @param int (mcrypt mode constant)
192 * @param string
193 * @return string
194 */
Greg Akerd1af1852011-12-25 21:59:30 -0600195 public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key = '')
Derek Jones09c77932010-08-31 13:17:10 -0500196 {
197 if ($this->_mcrypt_exists === FALSE)
198 {
199 log_message('error', 'Encoding from legacy is available only when Mcrypt is in use.');
200 return FALSE;
201 }
Andrey Andreev7c251b32011-12-27 16:37:23 +0200202 elseif (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
203 {
204 return FALSE;
205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Jones09c77932010-08-31 13:17:10 -0500207 // decode it first
208 // set mode temporarily to what it was when string was encoded with the legacy
Barry Mienydd671972010-10-04 16:33:58 +0200209 // algorithm - typically MCRYPT_MODE_ECB
Derek Jones09c77932010-08-31 13:17:10 -0500210 $current_mode = $this->_get_mode();
211 $this->set_mode($legacy_mode);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Jones09c77932010-08-31 13:17:10 -0500213 $key = $this->get_key($key);
Derek Jones09c77932010-08-31 13:17:10 -0500214 $dec = base64_decode($string);
Derek Jones09c77932010-08-31 13:17:10 -0500215 if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
216 {
Andrey Andreevf696c1f2012-06-12 12:14:51 +0300217 $this->set_mode($current_mode);
Derek Jones09c77932010-08-31 13:17:10 -0500218 return FALSE;
219 }
220
221 $dec = $this->_xor_decode($dec, $key);
222
223 // set the mcrypt mode back to what it should be, typically MCRYPT_MODE_CBC
Derek Jones092103e2010-09-02 11:11:58 -0500224 $this->set_mode($current_mode);
Derek Jones09c77932010-08-31 13:17:10 -0500225
226 // and re-encode
227 return base64_encode($this->mcrypt_encode($dec, $key));
228 }
229
230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 /**
233 * XOR Encode
234 *
235 * Takes a plain-text string and key as input and generates an
236 * encoded bit-string using XOR
237 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 * @param string
239 * @param string
240 * @return string
241 */
Greg Akerd1af1852011-12-25 21:59:30 -0600242 protected function _xor_encode($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
244 $rand = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200245 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
247 $rand .= mt_rand(0, mt_getrandmax());
248 }
Andrey Andreev7c251b32011-12-27 16:37:23 +0200249 while (strlen($rand) < 32);
Derek Allard2067d1a2008-11-13 22:59:24 +0000250
251 $rand = $this->hash($rand);
252
253 $enc = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200254 for ($i = 0, $ls = strlen($string), $lr = strlen($rand); $i < $ls; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200255 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200256 $enc .= $rand[($i % $lr)].($rand[($i % $lr)] ^ $string[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 return $this->_xor_merge($enc, $key);
260 }
261
262 // --------------------------------------------------------------------
263
264 /**
265 * XOR Decode
266 *
267 * Takes an encoded string and key as input and generates the
268 * plain-text original message
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_decode($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 $string = $this->_xor_merge($string, $key);
277
278 $dec = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200279 for ($i = 0, $l = strlen($string); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200281 $dec .= ($string[$i++] ^ $string[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283
284 return $dec;
285 }
286
287 // --------------------------------------------------------------------
288
289 /**
290 * XOR key + string Combiner
291 *
292 * Takes a string and key as input and computes the difference using XOR
293 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 * @param string
295 * @param string
296 * @return string
297 */
Greg Akerd1af1852011-12-25 21:59:30 -0600298 protected function _xor_merge($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
300 $hash = $this->hash($key);
301 $str = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200302 for ($i = 0, $ls = strlen($string), $lh = strlen($hash); $i < $ls; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200304 $str .= $string[$i] ^ $hash[($i % $lh)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
306
307 return $str;
308 }
309
310 // --------------------------------------------------------------------
311
312 /**
313 * Encrypt using Mcrypt
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @param string
316 * @param string
317 * @return string
318 */
Greg Akerd1af1852011-12-25 21:59:30 -0600319 public function mcrypt_encode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
322 $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
323 return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
324 }
325
326 // --------------------------------------------------------------------
327
328 /**
329 * Decrypt using Mcrypt
330 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @param string
332 * @param string
333 * @return string
334 */
Greg Akerd1af1852011-12-25 21:59:30 -0600335 public function mcrypt_decode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
337 $data = $this->_remove_cipher_noise($data, $key);
338 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
339
340 if ($init_size > strlen($data))
341 {
342 return FALSE;
343 }
344
345 $init_vect = substr($data, 0, $init_size);
346 $data = substr($data, $init_size);
347 return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
348 }
349
350 // --------------------------------------------------------------------
351
352 /**
353 * Adds permuted noise to the IV + encrypted data to protect
354 * against Man-in-the-middle attacks on CBC mode ciphers
355 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV
356 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 * @param string
358 * @param string
359 * @return string
360 */
Greg Akerd1af1852011-12-25 21:59:30 -0600361 protected function _add_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200363 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 $str = '';
365
Andrey Andreev7c251b32011-12-27 16:37:23 +0200366 for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200368 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 {
370 $j = 0;
371 }
372
Andrey Andreev7c251b32011-12-27 16:37:23 +0200373 $str .= chr((ord($data[$i]) + ord($key[$j])) % 256);
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
375
376 return $str;
377 }
378
379 // --------------------------------------------------------------------
380
381 /**
382 * Removes permuted noise from the IV + encrypted data, reversing
383 * _add_cipher_noise()
384 *
385 * Function description
386 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400387 * @param string $data
388 * @param string $key
Andrey Andreev38d0e932012-04-03 19:27:45 +0300389 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 */
Greg Akerd1af1852011-12-25 21:59:30 -0600391 protected function _remove_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200393 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 $str = '';
395
Andrey Andreev7c251b32011-12-27 16:37:23 +0200396 for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200398 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 {
400 $j = 0;
401 }
402
Andrey Andreev7c251b32011-12-27 16:37:23 +0200403 $temp = ord($data[$i]) - ord($key[$j]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000404
405 if ($temp < 0)
406 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200407 $temp += 256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 $str .= chr($temp);
411 }
412
413 return $str;
414 }
415
416 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 /**
419 * Set the Mcrypt Cipher
420 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300421 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500422 * @return CI_Encrypt
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 */
Greg Akerd1af1852011-12-25 21:59:30 -0600424 public function set_cipher($cipher)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
426 $this->_mcrypt_cipher = $cipher;
Greg Akerd1af1852011-12-25 21:59:30 -0600427 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429
430 // --------------------------------------------------------------------
431
432 /**
433 * Set the Mcrypt Mode
434 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300435 * @param int
Andrew Podner4296a652012-12-17 07:51:15 -0500436 * @return CI_Encrypt
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 */
Andrey Andreev7c251b32011-12-27 16:37:23 +0200438 public function set_mode($mode)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
440 $this->_mcrypt_mode = $mode;
Greg Akerd1af1852011-12-25 21:59:30 -0600441 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
443
444 // --------------------------------------------------------------------
445
446 /**
447 * Get Mcrypt cipher Value
448 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300449 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 */
Greg Akerd1af1852011-12-25 21:59:30 -0600451 protected function _get_cipher()
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300453 if ($this->_mcrypt_cipher === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200455 return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 }
457
458 return $this->_mcrypt_cipher;
459 }
460
461 // --------------------------------------------------------------------
462
463 /**
464 * Get Mcrypt Mode Value
465 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300466 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 */
Greg Akerd1af1852011-12-25 21:59:30 -0600468 protected function _get_mode()
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300470 if ($this->_mcrypt_mode === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200472 return $this->_mcrypt_mode = MCRYPT_MODE_CBC;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 return $this->_mcrypt_mode;
476 }
477
478 // --------------------------------------------------------------------
479
480 /**
481 * Set the Hash type
482 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @param string
Andrey Andreevf4cb94e2012-01-19 15:16:55 +0200484 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 */
Greg Akerd1af1852011-12-25 21:59:30 -0600486 public function set_hash($type = 'sha1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Daniel Morrisada77752012-10-04 10:24:16 +0100488 $this->_hash_type = in_array($type, hash_algos()) ? $type : 'sha1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490
491 // --------------------------------------------------------------------
492
493 /**
494 * Hash encode a string
495 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @param string
497 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200498 */
Greg Akerd1af1852011-12-25 21:59:30 -0600499 public function hash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Daniel Morrisa9923f52012-10-03 19:37:09 +0100501 return hash($this->_hash_type, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Andrey Andreev38d0e932012-04-03 19:27:45 +0300503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504}
505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506/* End of file Encrypt.php */
Andrey Andreev38d0e932012-04-03 19:27:45 +0300507/* Location: ./system/libraries/Encrypt.php */