Kohana_Encrypt
Constants
- None
Properties
Properties
-
public engine $_engine
-
Encryption engine
-
Default value:
NULL
-
public static string $default
-
default instance name
-
string(7) "default"
-
public static array $instances
-
Encrypt class instances
-
array(0)
Methods
public __construct(string $key_config [, string $mode = NULL , string $cipher = NULL ] ) (defined in Kohana_Encrypt)
Creates a new mcrypt wrapper.
Parameters
- string $key_config required - Encryption key or config array
- string $mode = NULL - Encryption mode
- string $cipher = NULL - Encryption cipher
Source Code
public function __construct($key_config, $mode = NULL, $cipher = NULL)
{
if (is_string($key_config))
{
$this->_engine = new Encrypt_Engine_Mcrypt($key_config, $mode, $cipher);
}
else
{
if ( ! isset($key_config['type']))
{
$key_config['type'] = 'mcrypt';
}
// Set the engine class name
$engine_name = 'Encrypt_Engine_'.ucfirst($key_config['type']);
// Create the engine class
$this->_engine = new $engine_name($key_config);
}
}
public decode(string $data ) (defined in Kohana_Encrypt)
Decrypts an encoded string back to its original value.
$data = $encrypt->decode($data);
Parameters
- string $data required - Encoded string to be decrypted
Return Values
- FALSE - If decryption fails
- string
Source Code
public function decode($data)
{
return $this->_engine->decrypt($data);
}
public encode(string $data ) (defined in Kohana_Encrypt)
Encrypts a string and returns an encrypted string that can be decoded.
$data = $encrypt->encode($data);
The encrypted binary data is encoded using base64 to convert it to a string. This string can be stored in a database, displayed, and passed using most other means without corruption.
Parameters
- string $data required - Data to be encrypted
Return Values
- string
Source Code
public function encode($data)
{
// Get an initialization vector
$iv = $this->_create_iv();
return $this->_engine->encrypt($data, $iv);
}
public static instance([ string $name = NULL , $config = NULL ] ) (defined in Kohana_Encrypt)
Returns a singleton instance of Encrypt. An encryption key must be provided in your "encrypt" configuration file.
$encrypt = Encrypt::instance();
Parameters
- string $name = NULL - Configuration group name
- unknown $config = NULL
Return Values
- Encrypt
Source Code
public static function instance($name = NULL, array $config = NULL)
{
if ($name === NULL)
{
// Use the default instance name
$name = Encrypt::$default;
}
if ( ! isset(Encrypt::$instances[$name]))
{
if ($config === NULL)
{
// Load the configuration data
$config = Kohana::$config->load('encrypt')->$name;
}
if ( ! isset($config['key']))
{
// No default encryption key is provided!
throw new Kohana_Exception('No encryption key is defined in the encryption configuration group: :group',
[':group' => $name]);
}
// Create a new instance
Encrypt::$instances[$name] = new Encrypt($config);
}
return Encrypt::$instances[$name];
}
protected _create_iv() (defined in Kohana_Encrypt)
Proxy for the mcrypt_create_iv function - to allow mocking and testing against KAT vectors
Return Values
- string - The initialization vector or FALSE on error
Source Code
protected function _create_iv()
{
return $this->_engine->create_iv();
}