blob: 3b04f7b0600bd6ae5044ca334fa9cf15bcc91a9f [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 {
Andrey Andreevf696c1f2012-06-12 12:14:51 +0300216 $this->set_mode($current_mode);
Derek Jones09c77932010-08-31 13:17:10 -0500217 return FALSE;
218 }
219
220 $dec = $this->_xor_decode($dec, $key);
221
222 // set the mcrypt mode back to what it should be, typically MCRYPT_MODE_CBC
Derek Jones092103e2010-09-02 11:11:58 -0500223 $this->set_mode($current_mode);
Derek Jones09c77932010-08-31 13:17:10 -0500224
225 // and re-encode
226 return base64_encode($this->mcrypt_encode($dec, $key));
227 }
228
229 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 /**
232 * XOR Encode
233 *
234 * Takes a plain-text string and key as input and generates an
235 * encoded bit-string using XOR
236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * @param string
238 * @param string
239 * @return string
240 */
Greg Akerd1af1852011-12-25 21:59:30 -0600241 protected function _xor_encode($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 {
243 $rand = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200244 do
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
246 $rand .= mt_rand(0, mt_getrandmax());
247 }
Andrey Andreev7c251b32011-12-27 16:37:23 +0200248 while (strlen($rand) < 32);
Derek Allard2067d1a2008-11-13 22:59:24 +0000249
250 $rand = $this->hash($rand);
251
252 $enc = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200253 for ($i = 0, $ls = strlen($string), $lr = strlen($rand); $i < $ls; $i++)
Barry Mienydd671972010-10-04 16:33:58 +0200254 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200255 $enc .= $rand[($i % $lr)].($rand[($i % $lr)] ^ $string[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
257
258 return $this->_xor_merge($enc, $key);
259 }
260
261 // --------------------------------------------------------------------
262
263 /**
264 * XOR Decode
265 *
266 * Takes an encoded string and key as input and generates the
267 * plain-text original message
268 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 * @param string
270 * @param string
271 * @return string
272 */
Greg Akerd1af1852011-12-25 21:59:30 -0600273 protected function _xor_decode($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
275 $string = $this->_xor_merge($string, $key);
276
277 $dec = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200278 for ($i = 0, $l = strlen($string); $i < $l; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200280 $dec .= ($string[$i++] ^ $string[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282
283 return $dec;
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * XOR key + string Combiner
290 *
291 * Takes a string and key as input and computes the difference using XOR
292 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 * @param string
294 * @param string
295 * @return string
296 */
Greg Akerd1af1852011-12-25 21:59:30 -0600297 protected function _xor_merge($string, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
299 $hash = $this->hash($key);
300 $str = '';
Andrey Andreev7c251b32011-12-27 16:37:23 +0200301 for ($i = 0, $ls = strlen($string), $lh = strlen($hash); $i < $ls; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200303 $str .= $string[$i] ^ $hash[($i % $lh)];
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
305
306 return $str;
307 }
308
309 // --------------------------------------------------------------------
310
311 /**
312 * Encrypt using Mcrypt
313 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 * @param string
315 * @param string
316 * @return string
317 */
Greg Akerd1af1852011-12-25 21:59:30 -0600318 public function mcrypt_encode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
321 $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
322 return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
323 }
324
325 // --------------------------------------------------------------------
326
327 /**
328 * Decrypt using Mcrypt
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @param string
331 * @param string
332 * @return string
333 */
Greg Akerd1af1852011-12-25 21:59:30 -0600334 public function mcrypt_decode($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 {
336 $data = $this->_remove_cipher_noise($data, $key);
337 $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
338
339 if ($init_size > strlen($data))
340 {
341 return FALSE;
342 }
343
344 $init_vect = substr($data, 0, $init_size);
345 $data = substr($data, $init_size);
346 return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
347 }
348
349 // --------------------------------------------------------------------
350
351 /**
352 * Adds permuted noise to the IV + encrypted data to protect
353 * against Man-in-the-middle attacks on CBC mode ciphers
354 * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @param string
357 * @param string
358 * @return string
359 */
Greg Akerd1af1852011-12-25 21:59:30 -0600360 protected function _add_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200362 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 $str = '';
364
Andrey Andreev7c251b32011-12-27 16:37:23 +0200365 for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200367 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
369 $j = 0;
370 }
371
Andrey Andreev7c251b32011-12-27 16:37:23 +0200372 $str .= chr((ord($data[$i]) + ord($key[$j])) % 256);
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 }
374
375 return $str;
376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * Removes permuted noise from the IV + encrypted data, reversing
382 * _add_cipher_noise()
383 *
384 * Function description
385 *
Timothy Warren0688ac92012-04-20 10:25:04 -0400386 * @param string $data
387 * @param string $key
Andrey Andreev38d0e932012-04-03 19:27:45 +0300388 * @return string
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 */
Greg Akerd1af1852011-12-25 21:59:30 -0600390 protected function _remove_cipher_noise($data, $key)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200392 $key = $this->hash($key);
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 $str = '';
394
Andrey Andreev7c251b32011-12-27 16:37:23 +0200395 for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200397 if ($j >= $lk)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 $j = 0;
400 }
401
Andrey Andreev7c251b32011-12-27 16:37:23 +0200402 $temp = ord($data[$i]) - ord($key[$j]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403
404 if ($temp < 0)
405 {
Andrey Andreev7c251b32011-12-27 16:37:23 +0200406 $temp += 256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 $str .= chr($temp);
410 }
411
412 return $str;
413 }
414
415 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 /**
418 * Set the Mcrypt Cipher
419 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300420 * @param int
421 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 */
Greg Akerd1af1852011-12-25 21:59:30 -0600423 public function set_cipher($cipher)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
425 $this->_mcrypt_cipher = $cipher;
Greg Akerd1af1852011-12-25 21:59:30 -0600426 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Set the Mcrypt Mode
433 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300434 * @param int
435 * @return object
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 */
Andrey Andreev7c251b32011-12-27 16:37:23 +0200437 public function set_mode($mode)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 $this->_mcrypt_mode = $mode;
Greg Akerd1af1852011-12-25 21:59:30 -0600440 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
442
443 // --------------------------------------------------------------------
444
445 /**
446 * Get Mcrypt cipher Value
447 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300448 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 */
Greg Akerd1af1852011-12-25 21:59:30 -0600450 protected function _get_cipher()
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300452 if ($this->_mcrypt_cipher === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200454 return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256;
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456
457 return $this->_mcrypt_cipher;
458 }
459
460 // --------------------------------------------------------------------
461
462 /**
463 * Get Mcrypt Mode Value
464 *
Andrey Andreev38d0e932012-04-03 19:27:45 +0300465 * @return int
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 */
Greg Akerd1af1852011-12-25 21:59:30 -0600467 protected function _get_mode()
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
Andrey Andreev79eca3d2012-06-04 18:28:50 +0300469 if ($this->_mcrypt_mode === NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Andrey Andreevd655a992012-01-10 22:31:29 +0200471 return $this->_mcrypt_mode = MCRYPT_MODE_CBC;
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 return $this->_mcrypt_mode;
475 }
476
477 // --------------------------------------------------------------------
478
479 /**
480 * Set the Hash type
481 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 * @param string
Andrey Andreevf4cb94e2012-01-19 15:16:55 +0200483 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 */
Greg Akerd1af1852011-12-25 21:59:30 -0600485 public function set_hash($type = 'sha1')
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Daniel Morrisa9923f52012-10-03 19:37:09 +0100487 $this->_hash_type = (in_array($type, hash_algos())) ? $type : 'sha1';
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
490 // --------------------------------------------------------------------
491
492 /**
493 * Hash encode a string
494 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 * @param string
496 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200497 */
Greg Akerd1af1852011-12-25 21:59:30 -0600498 public function hash($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
Daniel Morrisa9923f52012-10-03 19:37:09 +0100500 return hash($this->_hash_type, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Andrey Andreev38d0e932012-04-03 19:27:45 +0300502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503}
504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505/* End of file Encrypt.php */
Andrey Andreev38d0e932012-04-03 19:27:45 +0300506/* Location: ./system/libraries/Encrypt.php */