Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | f4cb94e | 2012-01-19 15:16:55 +0200 | [diff] [blame^] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/libraries/encryption.html |
| 38 | */ |
| 39 | class CI_Encrypt { |
| 40 | |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 41 | public $encryption_key = ''; |
| 42 | protected $_hash_type = 'sha1'; |
| 43 | protected $_mcrypt_exists = FALSE; |
| 44 | protected $_mcrypt_cipher; |
| 45 | protected $_mcrypt_mode; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 47 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | $this->_mcrypt_exists = ( ! function_exists('mcrypt_encrypt')) ? FALSE : TRUE; |
Andrey Andreev | cc6dbda | 2012-01-08 06:35:17 +0200 | [diff] [blame] | 50 | log_message('debug', 'Encrypt Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
| 54 | |
| 55 | /** |
| 56 | * Fetch the encryption key |
| 57 | * |
| 58 | * Returns it as MD5 in order to have an exact-length 128 bit key. |
| 59 | * Mcrypt is sensitive to keys that are not the correct length |
| 60 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | * @param string |
| 62 | * @return string |
| 63 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 64 | public function get_key($key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | { |
| 66 | if ($key == '') |
| 67 | { |
| 68 | if ($this->encryption_key != '') |
| 69 | { |
| 70 | return $this->encryption_key; |
| 71 | } |
| 72 | |
| 73 | $CI =& get_instance(); |
| 74 | $key = $CI->config->item('encryption_key'); |
| 75 | |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 76 | if ($key === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | { |
| 78 | show_error('In order to use the encryption class requires that you set an encryption key in your config file.'); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return md5($key); |
| 83 | } |
| 84 | |
| 85 | // -------------------------------------------------------------------- |
| 86 | |
| 87 | /** |
| 88 | * Set the encryption key |
| 89 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | * @param string |
Andrey Andreev | cc6dbda | 2012-01-08 06:35:17 +0200 | [diff] [blame] | 91 | * @return object |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 93 | public function set_key($key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
| 95 | $this->encryption_key = $key; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 96 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // -------------------------------------------------------------------- |
| 100 | |
| 101 | /** |
| 102 | * Encode |
| 103 | * |
| 104 | * Encodes the message string using bitwise XOR encoding. |
| 105 | * The key is combined with a random hash, and then it |
| 106 | * too gets converted using XOR. The whole thing is then run |
| 107 | * through mcrypt (if supported) using the randomized key. |
| 108 | * The end result is a double-encrypted message string |
| 109 | * that is randomized with each call to this function, |
| 110 | * even if the supplied message and key are the same. |
| 111 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | * @param string the string to encode |
| 113 | * @param string the key |
| 114 | * @return string |
| 115 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 116 | public function encode($string, $key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 118 | $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_encode' : '_xor_encode'; |
| 119 | return base64_encode($this->$method($string, $this->get_key($key))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // -------------------------------------------------------------------- |
| 123 | |
| 124 | /** |
| 125 | * Decode |
| 126 | * |
| 127 | * Reverses the above process |
| 128 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | * @param string |
| 130 | * @param string |
| 131 | * @return string |
| 132 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 133 | public function decode($string, $key = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string)) |
| 136 | { |
| 137 | return FALSE; |
| 138 | } |
| 139 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 140 | $method = ($this->_mcrypt_exists === TRUE) ? 'mcrypt_decode' : '_xor_decode'; |
| 141 | return $this->$method(base64_decode($string), $this->get_key($key)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 146 | /** |
| 147 | * Encode from Legacy |
| 148 | * |
| 149 | * Takes an encoded string from the original Encryption class algorithms and |
| 150 | * returns a newly encoded string using the improved method added in 2.0.0 |
| 151 | * This allows for backwards compatibility and a method to transition to the |
| 152 | * new encryption algorithms. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | * |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 154 | * For more details, see http://codeigniter.com/user_guide/installation/upgrade_200.html#encryption |
| 155 | * |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 156 | * @param string |
| 157 | * @param int (mcrypt mode constant) |
| 158 | * @param string |
| 159 | * @return string |
| 160 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 161 | public function encode_from_legacy($string, $legacy_mode = MCRYPT_MODE_ECB, $key = '') |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 162 | { |
| 163 | if ($this->_mcrypt_exists === FALSE) |
| 164 | { |
| 165 | log_message('error', 'Encoding from legacy is available only when Mcrypt is in use.'); |
| 166 | return FALSE; |
| 167 | } |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 168 | elseif (preg_match('/[^a-zA-Z0-9\/\+=]/', $string)) |
| 169 | { |
| 170 | return FALSE; |
| 171 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 173 | // decode it first |
| 174 | // set mode temporarily to what it was when string was encoded with the legacy |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | // algorithm - typically MCRYPT_MODE_ECB |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 176 | $current_mode = $this->_get_mode(); |
| 177 | $this->set_mode($legacy_mode); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 179 | $key = $this->get_key($key); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 180 | $dec = base64_decode($string); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 181 | if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE) |
| 182 | { |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | $dec = $this->_xor_decode($dec, $key); |
| 187 | |
| 188 | // set the mcrypt mode back to what it should be, typically MCRYPT_MODE_CBC |
Derek Jones | 092103e | 2010-09-02 11:11:58 -0500 | [diff] [blame] | 189 | $this->set_mode($current_mode); |
Derek Jones | 09c7793 | 2010-08-31 13:17:10 -0500 | [diff] [blame] | 190 | |
| 191 | // and re-encode |
| 192 | return base64_encode($this->mcrypt_encode($dec, $key)); |
| 193 | } |
| 194 | |
| 195 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | /** |
| 198 | * XOR Encode |
| 199 | * |
| 200 | * Takes a plain-text string and key as input and generates an |
| 201 | * encoded bit-string using XOR |
| 202 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | * @param string |
| 204 | * @param string |
| 205 | * @return string |
| 206 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 207 | protected function _xor_encode($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | { |
| 209 | $rand = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 210 | do |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | { |
| 212 | $rand .= mt_rand(0, mt_getrandmax()); |
| 213 | } |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 214 | while (strlen($rand) < 32); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | |
| 216 | $rand = $this->hash($rand); |
| 217 | |
| 218 | $enc = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 219 | for ($i = 0, $ls = strlen($string), $lr = strlen($rand); $i < $ls; $i++) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 221 | $enc .= $rand[($i % $lr)].($rand[($i % $lr)] ^ $string[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return $this->_xor_merge($enc, $key); |
| 225 | } |
| 226 | |
| 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * XOR Decode |
| 231 | * |
| 232 | * Takes an encoded string and key as input and generates the |
| 233 | * plain-text original message |
| 234 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | * @param string |
| 236 | * @param string |
| 237 | * @return string |
| 238 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 239 | protected function _xor_decode($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | { |
| 241 | $string = $this->_xor_merge($string, $key); |
| 242 | |
| 243 | $dec = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 244 | for ($i = 0, $l = strlen($string); $i < $l; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 246 | $dec .= ($string[$i++] ^ $string[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return $dec; |
| 250 | } |
| 251 | |
| 252 | // -------------------------------------------------------------------- |
| 253 | |
| 254 | /** |
| 255 | * XOR key + string Combiner |
| 256 | * |
| 257 | * Takes a string and key as input and computes the difference using XOR |
| 258 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | * @param string |
| 260 | * @param string |
| 261 | * @return string |
| 262 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 263 | protected function _xor_merge($string, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
| 265 | $hash = $this->hash($key); |
| 266 | $str = ''; |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 267 | for ($i = 0, $ls = strlen($string), $lh = strlen($hash); $i < $ls; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 269 | $str .= $string[$i] ^ $hash[($i % $lh)]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return $str; |
| 273 | } |
| 274 | |
| 275 | // -------------------------------------------------------------------- |
| 276 | |
| 277 | /** |
| 278 | * Encrypt using Mcrypt |
| 279 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | * @param string |
| 281 | * @param string |
| 282 | * @return string |
| 283 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 284 | public function mcrypt_encode($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode()); |
| 287 | $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND); |
| 288 | return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key); |
| 289 | } |
| 290 | |
| 291 | // -------------------------------------------------------------------- |
| 292 | |
| 293 | /** |
| 294 | * Decrypt using Mcrypt |
| 295 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | * @param string |
| 297 | * @param string |
| 298 | * @return string |
| 299 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 300 | public function mcrypt_decode($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | { |
| 302 | $data = $this->_remove_cipher_noise($data, $key); |
| 303 | $init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode()); |
| 304 | |
| 305 | if ($init_size > strlen($data)) |
| 306 | { |
| 307 | return FALSE; |
| 308 | } |
| 309 | |
| 310 | $init_vect = substr($data, 0, $init_size); |
| 311 | $data = substr($data, $init_size); |
| 312 | return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0"); |
| 313 | } |
| 314 | |
| 315 | // -------------------------------------------------------------------- |
| 316 | |
| 317 | /** |
| 318 | * Adds permuted noise to the IV + encrypted data to protect |
| 319 | * against Man-in-the-middle attacks on CBC mode ciphers |
| 320 | * http://www.ciphersbyritter.com/GLOSSARY.HTM#IV |
| 321 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | * @param string |
| 323 | * @param string |
| 324 | * @return string |
| 325 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 326 | protected function _add_cipher_noise($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 328 | $key = $this->hash($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | $str = ''; |
| 330 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 331 | for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 333 | if ($j >= $lk) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | { |
| 335 | $j = 0; |
| 336 | } |
| 337 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 338 | $str .= chr((ord($data[$i]) + ord($key[$j])) % 256); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return $str; |
| 342 | } |
| 343 | |
| 344 | // -------------------------------------------------------------------- |
| 345 | |
| 346 | /** |
| 347 | * Removes permuted noise from the IV + encrypted data, reversing |
| 348 | * _add_cipher_noise() |
| 349 | * |
| 350 | * Function description |
| 351 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | * @param type |
| 353 | * @return type |
| 354 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 355 | protected function _remove_cipher_noise($data, $key) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 357 | $key = $this->hash($key); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | $str = ''; |
| 359 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 360 | for ($i = 0, $j = 0, $ld = strlen($data), $lk = strlen($key); $i < $ld; ++$i, ++$j) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 362 | if ($j >= $lk) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | { |
| 364 | $j = 0; |
| 365 | } |
| 366 | |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 367 | $temp = ord($data[$i]) - ord($key[$j]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | |
| 369 | if ($temp < 0) |
| 370 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 371 | $temp += 256; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | $str .= chr($temp); |
| 375 | } |
| 376 | |
| 377 | return $str; |
| 378 | } |
| 379 | |
| 380 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 381 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | /** |
| 383 | * Set the Mcrypt Cipher |
| 384 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | * @param constant |
| 386 | * @return string |
| 387 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 388 | public function set_cipher($cipher) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | { |
| 390 | $this->_mcrypt_cipher = $cipher; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 391 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | // -------------------------------------------------------------------- |
| 395 | |
| 396 | /** |
| 397 | * Set the Mcrypt Mode |
| 398 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | * @param constant |
| 400 | * @return string |
| 401 | */ |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 402 | public function set_mode($mode) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | { |
| 404 | $this->_mcrypt_mode = $mode; |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 405 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // -------------------------------------------------------------------- |
| 409 | |
| 410 | /** |
| 411 | * Get Mcrypt cipher Value |
| 412 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | * @return string |
| 414 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 415 | protected function _get_cipher() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
| 417 | if ($this->_mcrypt_cipher == '') |
| 418 | { |
Andrey Andreev | d655a99 | 2012-01-10 22:31:29 +0200 | [diff] [blame] | 419 | return $this->_mcrypt_cipher = MCRYPT_RIJNDAEL_256; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return $this->_mcrypt_cipher; |
| 423 | } |
| 424 | |
| 425 | // -------------------------------------------------------------------- |
| 426 | |
| 427 | /** |
| 428 | * Get Mcrypt Mode Value |
| 429 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | * @return string |
| 431 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 432 | protected function _get_mode() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
| 434 | if ($this->_mcrypt_mode == '') |
| 435 | { |
Andrey Andreev | d655a99 | 2012-01-10 22:31:29 +0200 | [diff] [blame] | 436 | return $this->_mcrypt_mode = MCRYPT_MODE_CBC; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | return $this->_mcrypt_mode; |
| 440 | } |
| 441 | |
| 442 | // -------------------------------------------------------------------- |
| 443 | |
| 444 | /** |
| 445 | * Set the Hash type |
| 446 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 447 | * @param string |
Andrey Andreev | f4cb94e | 2012-01-19 15:16:55 +0200 | [diff] [blame^] | 448 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 450 | public function set_hash($type = 'sha1') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | { |
Andrey Andreev | cc6dbda | 2012-01-08 06:35:17 +0200 | [diff] [blame] | 452 | $this->_hash_type = ($type !== 'sha1' && $type !== 'md5') ? 'sha1' : $type; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // -------------------------------------------------------------------- |
| 456 | |
| 457 | /** |
| 458 | * Hash encode a string |
| 459 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | * @param string |
| 461 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 462 | */ |
Greg Aker | d1af185 | 2011-12-25 21:59:30 -0600 | [diff] [blame] | 463 | public function hash($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | { |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 465 | return ($this->_hash_type === 'sha1') ? sha1($str) : md5($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 466 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | /* End of file Encrypt.php */ |
Andrey Andreev | 7c251b3 | 2011-12-27 16:37:23 +0200 | [diff] [blame] | 470 | /* Location: ./system/libraries/Encrypt.php */ |