1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * The Encrypt library provides two-way encryption of text and binary strings
4 * using the MCrypt extension.
5 * @see http://php.net/mcrypt
7 * $Id: Encrypt.php 3917 2009-01-21 03:06:22Z zombor $
11 * @copyright (c) 2007-2008 Kohana Team
12 * @license http://kohanaphp.com/license.html
16 // OS-dependant RAND type to use
17 protected static $rand;
23 * Returns a singleton instance of Encrypt.
25 * @param array configuration options
28 public static function instance($config = NULL)
32 // Create the singleton
33 empty($instance) and $instance = new Encrypt((array) $config);
39 * Loads encryption configuration and validates the data.
41 * @param array|string custom configuration or config group name
42 * @throws Kohana_Exception
44 public function __construct($config = FALSE)
46 if ( ! defined('MCRYPT_ENCRYPT'))
47 throw new Kohana_Exception('encrypt.requires_mcrypt');
49 if (is_string($config))
53 // Test the config group name
54 if (($config = Kohana
::config('encryption.'.$config)) === NULL)
55 throw new Kohana_Exception('encrypt.undefined_group', $name);
58 if (is_array($config))
60 // Append the default configuration options
61 $config +
= Kohana
::config('encryption.default');
65 // Load the default group
66 $config = Kohana
::config('encryption.default');
69 if (empty($config['key']))
70 throw new Kohana_Exception('encrypt.no_encryption_key');
72 // Find the max length of the key, based on cipher and mode
73 $size = mcrypt_get_key_size($config['cipher'], $config['mode']);
75 if (strlen($config['key']) > $size)
77 // Shorten the key to the maximum size
78 $config['key'] = substr($config['key'], 0, $size);
81 // Find the initialization vector size
82 $config['iv_size'] = mcrypt_get_iv_size($config['cipher'], $config['mode']);
84 // Cache the config in the object
85 $this->config
= $config;
87 Kohana
::log('debug', 'Encrypt Library initialized');
91 * Encrypts a string and returns an encrypted string that can be decoded.
93 * @param string data to be encrypted
94 * @return string encrypted data
96 public function encode($data)
98 // Set the rand type if it has not already been set
99 if (self
::$rand === NULL)
103 // Windows only supports the system random number generator
104 self
::$rand = MCRYPT_RAND
;
108 if (defined('MCRYPT_DEV_URANDOM'))
111 self
::$rand = MCRYPT_DEV_URANDOM
;
113 elseif (defined('MCRYPT_DEV_RANDOM'))
116 self
::$rand = MCRYPT_DEV_RANDOM
;
120 // Use the system random number generator
121 self
::$rand = MCRYPT_RAND
;
126 if (self
::$rand === MCRYPT_RAND
)
128 // The system random number generator must always be seeded each
129 // time it is used, or it will not produce true random results
133 // Create a random initialization vector of the proper size for the current cipher
134 $iv = mcrypt_create_iv($this->config
['iv_size'], self
::$rand);
136 // Encrypt the data using the configured options and generated iv
137 $data = mcrypt_encrypt($this->config
['cipher'], $this->config
['key'], $data, $this->config
['mode'], $iv);
139 // Use base64 encoding to convert to a string
140 return base64_encode($iv.$data);
144 * Decrypts an encoded string back to its original value.
146 * @param string encoded string to be decrypted
147 * @return string decrypted data
149 public function decode($data)
151 // Convert the data back to binary
152 $data = base64_decode($data);
154 // Extract the initialization vector from the data
155 $iv = substr($data, 0, $this->config
['iv_size']);
157 // Remove the iv from the data
158 $data = substr($data, $this->config
['iv_size']);
160 // Return the decrypted data, trimming the \0 padding bytes from the end of the data
161 return rtrim(mcrypt_decrypt($this->config
['cipher'], $this->config
['key'], $data, $this->config
['mode'], $iv), "\0");