blob: ce5e030b0748935ca147a644ca4efdce26ac2510 [file] [log] [blame]
Andrey Andreev7c251b32011-12-27 16:37:23 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Encryption Class
30 *
31 * Provides two-way keyed encoding using XOR Hashing and Mcrypt
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/encryption.html
38 */
39class CI_Encrypt {
40
Timothy Warren0688ac92012-04-20 10:25:04 -040041 /**
42 * Reference to the user's encryption key
43 *
44 * @var string
45 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030046 public $encryption_key = '';
Andrey Andreev56454792012-05-17 14:32:19 +030047
Timothy Warren0688ac92012-04-20 10:25:04 -040048 /**
49 * Type of hash operation
Andrey Andreev56454792012-05-17 14:32:19 +030050 *
Timothy Warren0688ac92012-04-20 10:25:04 -040051 * @var string
52 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030053 protected $_hash_type = 'sha1';
Andrey Andreev56454792012-05-17 14:32:19 +030054
Timothy Warren0688ac92012-04-20 10:25:04 -040055 /**
56 * Flag for the existance of mcrypt
57 *
58 * @var bool
59 */
Andrey Andreev38d0e932012-04-03 19:27:45 +030060 protected $_mcrypt_exists = FALSE;
Andrey Andreev56454792012-05-17 14:32:19 +030061
Timothy Warren0688ac92012-04-20 10:25:04 -040062 /**
63 * Current cipher to be used with mcrypt
64 *
65 * @var string
66 */
Greg Akerd1af1852011-12-25 21:59:30 -060067 protected $_mcrypt_cipher;
Andrey Andreev56454792012-05-17 14:32:19 +030068
Timothy Warren0688ac92012-04-20 10:25:04 -040069 /**
70 * Method for encrypting/decrypting data
71 *
72 * @var int
73 */
Greg Akerd1af1852011-12-25 21:59:30 -060074 protected $_mcrypt_mode;
Derek Allard2067d1a2008-11-13 22:59:24 +000075
Timothy Warren0688ac92012-04-20 10:25:04 -040076 /**
77 * Initialize Encryption class
Andrey Andreev56454792012-05-17 14:32:19 +030078 *
79 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040080 */
Greg Akera9263282010-11-10 15:26:43 -060081 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
Andrey Andreev38d0e932012-04-03 19:27:45 +030083 $this->_mcrypt_exists = function_exists('mcrypt_encrypt');
Andrey Andreevcc6dbda2012-01-08 06:35:17 +020084 log_message('debug', 'Encrypt Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
86
87 // --------------------------------------------------------------------
88
89 /**
90 * Fetch the encryption key
91 *
92 * Returns it as MD5 in order to have an exact-length 128 bit key.
93 * Mcrypt is sensitive to keys that are not the correct length
94 *
Derek Allard2067d1a2008-11-13 22:59:24 +000095 * @param string
96 * @return string
97 */
Greg Akerd1af1852011-12-25 21:59:30 -060098 public function get_key($key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100100 if ($key === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100102 if ($this->encryption_key !== '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 {
104 return $this->encryption_key;
105 }
106
Joffrey Jaffeuxba7f50b2012-06-06 01:40:01 +0200107 $key = config_item('encryption_key');
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Greg Akerd1af1852011-12-25 21:59:30 -0600109 if ($key === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 show_error('In order to use the encryption class requires that you set an encryption key in your config file.');
112 }
113 }
114
115 return md5($key);
116 }
117
118 // --------------------------------------------------------------------
119
120 /**
121 * Set the encryption key
122 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 * @param string
Andrey Andreevcc6dbda2012-01-08 06:35:17 +0200124 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 */
Greg Akerd1af1852011-12-25 21:59:30 -0600126 public function set_key($key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 $this->encryption_key = $key;
Greg Akerd1af1852011-12-25 21:59:30 -0600129 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Encode
136 *
137 * Encodes the message string using bitwise XOR encoding.
138 * The key is combined with a random hash, and then it
139 * too gets converted using XOR. The whole thing is then run
140 * through mcrypt (if supported) using the randomized key.
141 * The end result is a double-encrypted message string
142 * that is randomized with each call to this function,
143 * even if the supplied message and key are the same.
144 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * @param string the string to encode
146 * @param string the key
147 * @return string
148 */
Greg Akerd1af1852011-12-25 21:59:30 -0600149 public function encode($string, $key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200151 $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_encode' : '_xor_encode';
152 return base64_encode($this->$method($string, $this->get_key($key)));
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154
155 // --------------------------------------------------------------------
156
157 /**
158 * Decode
159 *
160 * Reverses the above process
161 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 * @param string
163 * @param string
164 * @return string
165 */
Greg Akerd1af1852011-12-25 21:59:30 -0600166 public function decode($string, $key = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
169 {
170 return FALSE;
171 }
172
Andrey Andreev7c251b32011-12-27 16:37:23 +0200173 $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_decode' : '_xor_decode';
174 return $this->$method(base64_decode($string), $this->get_key($key));
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 }
176
177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Jones09c77932010-08-31 13:17:10 -0500179 /**
180 * Encode from Legacy
181 *
182 * Takes an encoded string from the original Encryption class algorithms and
183 * returns a newly encoded string using the improved method added in 2.0.0
184 * This allows for backwards compatibility and a method to transition to the
185 * new encryption algorithms.
Barry Mienydd671972010-10-04 16:33:58 +0200186 *
Derek Jones09c77932010-08-31 13:17:10 -0500187 * For more details, see http://codeigniter.com/user_guide/installation/upgrade_200.html#encryption
188 *
Derek Jones09c77932010-08-31 13:17:10 -0500189 * @param string
190 * @param int (mcrypt mode constant)
191 * @param string
192 * @return string
193 */
Greg Akerd1af1852011-12-25 21:59:30 -0600194 public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key = '')
Derek Jones09c77932010-08-31 13:17:10 -0500195 {
196 if ($this->_mcrypt_exists === FALSE)
197 {
198 log_message('error', 'Encoding from legacy is available only when Mcrypt is in use.');
199 return FALSE;
200 }
Andrey Andreev7c251b32011-12-27 16:37:23 +0200201 elseif (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
202 {
203 return FALSE;
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Jones09c77932010-08-31 13:17:10 -0500206 // decode it first
207 // set mode temporarily to what it was when string was encoded with the legacy
Barry Mienydd671972010-10-04 16:33:58 +0200208 // algorithm - typically MCRYPT_MODE_ECB
Derek Jones09c77932010-08-31 13:17:10 -0500209 $current_mode = $this->_get_mode();
210 $this->set_mode($legacy_mode);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Jones09c77932010-08-31 13:17:10 -0500212 $key = $this->get_key($key);
Derek Jones09c77932010-08-31 13:17:10 -0500213 $dec = base64_decode($string);
Derek Jones09c77932010-08-31 13:17:10 -0500214 if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
215 {
216 return FALSE;
217 }
218
219 $dec = $this->_xor_decode($dec, $key);
220
221 // set the mcrypt mode back to what it should be, typically MCRYPT_MODE_CBC
Derek Jones092103e2010-09-02 11:11:58 -0500222 $this->set_mode($current_mode);
Derek Jones09c77932010-08-31 13:17:10 -0500223
224 // and re-encode
225 return base64_encode($this->mcrypt_encode($dec, $key));
226 }
227
228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 /**
231 * XOR Encode
232 *
233 * Takes a plain-text string and key as input and generates an
234 * encoded bit-string using XOR
235 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 * @param string
237 * @param string
238 * @return string
239 */
Greg Akerd1af1852011-12-25 21:59:30 -0600240 protected function _xor_encode($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
242 $rand = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200243 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 $rand .= mt_rand(0, mt_getrandmax());
246 }
Andrey Andreev7c251b32011-12-27 16:37:23 +0200247 while (strlen($rand) < 32);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
249 $rand = $this->hash($rand);
250
251 $enc = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200252 for ($i = 0, $ls = strlen($string), $lr = strlen($rand); $i < $ls; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200253 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200254 $enc .= $rand[($i % $lr)].($rand[($i % $lr)] ^ $string[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256
257 return $this->_xor_merge($enc, $key);
258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * XOR Decode
264 *
265 * Takes an encoded string and key as input and generates the
266 * plain-text original message
267 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 * @param string
269 * @param string
270 * @return string
271 */
Greg Akerd1af1852011-12-25 21:59:30 -0600272 protected function _xor_decode($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 $string = $this->_xor_merge($string, $key);
275
276 $dec = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200277 for ($i = 0, $l = strlen($string); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200279 $dec .= ($string[$i++] ^ $string[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
281
282 return $dec;
283 }
284
285 // --------------------------------------------------------------------
286
287 /**
288 * XOR key + string Combiner
289 *
290 * Takes a string and key as input and computes the difference using XOR
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 protected function _xor_merge($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 $hash = $this->hash($key);
299 $str = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200300 for ($i = 0, $ls = strlen($string), $lh = strlen($hash); $i < $ls; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200302 $str .= $string[$i] ^ $hash[($i % $lh)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304
305 return $str;
306 }
307
308 // --------------------------------------------------------------------
309
310 /**
311 * Encrypt using Mcrypt
312 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * @param string
314 * @param string
315 * @return string
316 */
Greg Akerd1af1852011-12-25 21:59:30 -0600317 public function mcrypt_encode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
320 $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
321 return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * Decrypt using Mcrypt
328 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 * @param string
330 * @param string
331 * @return string
332 */
Greg Akerd1af1852011-12-25 21:59:30 -0600333 public function mcrypt_decode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
335 $data = $this->_remove_cipher_noise($data, $key);
336 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
337
338 if ($init_size > strlen($data))
339 {
340 return FALSE;
341 }
342
343 $init_vect = substr($data, 0, $init_size);
344 $data = substr($data, $init_size);
345 return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
346 }
347
348 // --------------------------------------------------------------------
349
350 /**
351 * Adds permuted noise to the IV + encrypted data to protect
352 * against Man-in-the-middle attacks on CBC mode ciphers
353 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @param string
356 * @param string
357 * @return string
358 */
Greg Akerd1af1852011-12-25 21:59:30 -0600359 protected function _add_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200361 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 $str = '';
363
Andrey Andreev7c251b32011-12-27 16:37:23 +0200364 for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200366 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 {
368 $j = 0;
369 }
370
Andrey Andreev7c251b32011-12-27 16:37:23 +0200371 $str .= chr((ord($data[$i]) + ord($key[$j])) % 256);
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
373
374 return $str;
375 }
376
377 // --------------------------------------------------------------------
378
379 /**
380 * Removes permuted noise from the IV + encrypted data, reversing
381 * _add_cipher_noise()
382 *
383 * Function description
384 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400385 * @param string $data
386 * @param string $key
Andrey Andreev38d0e932012-04-03 19:27:45 +0300387 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 */
Greg Akerd1af1852011-12-25 21:59:30 -0600389 protected function _remove_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200391 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $str = '';
393
Andrey Andreev7c251b32011-12-27 16:37:23 +0200394 for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200396 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 {
398 $j = 0;
399 }
400
Andrey Andreev7c251b32011-12-27 16:37:23 +0200401 $temp = ord($data[$i]) - ord($key[$j]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000402
403 if ($temp < 0)
404 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200405 $temp += 256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 $str .= chr($temp);
409 }
410
411 return $str;
412 }
413
414 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 /**
417 * Set the Mcrypt Cipher
418 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300419 * @param int
420 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 */
Greg Akerd1af1852011-12-25 21:59:30 -0600422 public function set_cipher($cipher)
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 {
424 $this->_mcrypt_cipher = $cipher;
Greg Akerd1af1852011-12-25 21:59:30 -0600425 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
427
428 // --------------------------------------------------------------------
429
430 /**
431 * Set the Mcrypt Mode
432 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300433 * @param int
434 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 */
Andrey Andreev7c251b32011-12-27 16:37:23 +0200436 public function set_mode($mode)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 $this->_mcrypt_mode = $mode;
Greg Akerd1af1852011-12-25 21:59:30 -0600439 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
441
442 // --------------------------------------------------------------------
443
444 /**
445 * Get Mcrypt cipher Value
446 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300447 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 */
Greg Akerd1af1852011-12-25 21:59:30 -0600449 protected function _get_cipher()
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300451 if ($this->_mcrypt_cipher === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200453 return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 }
455
456 return $this->_mcrypt_cipher;
457 }
458
459 // --------------------------------------------------------------------
460
461 /**
462 * Get Mcrypt Mode Value
463 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300464 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 */
Greg Akerd1af1852011-12-25 21:59:30 -0600466 protected function _get_mode()
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300468 if ($this->_mcrypt_mode === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200470 return $this->_mcrypt_mode = MCRYPT_MODE_CBC;
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 return $this->_mcrypt_mode;
474 }
475
476 // --------------------------------------------------------------------
477
478 /**
479 * Set the Hash type
480 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @param string
Andrey Andreevf4cb94e2012-01-19 15:16:55 +0200482 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 */
Greg Akerd1af1852011-12-25 21:59:30 -0600484 public function set_hash($type = 'sha1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
Andrey Andreevcc6dbda2012-01-08 06:35:17 +0200486 $this->_hash_type = ($type !== 'sha1' && $type !== 'md5') ? 'sha1' : $type;
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 }
488
489 // --------------------------------------------------------------------
490
491 /**
492 * Hash encode a string
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string
495 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200496 */
Greg Akerd1af1852011-12-25 21:59:30 -0600497 public function hash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200499 return ($this->_hash_type === 'sha1') ? sha1($str) : md5($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Andrey Andreev38d0e932012-04-03 19:27:45 +0300501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502}
503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504/* End of file Encrypt.php */
Andrey Andreev38d0e932012-04-03 19:27:45 +0300505/* Location: ./system/libraries/Encrypt.php */