Crypt Class

The Crypt class allows encrypt or decrypt a string. The Crypt class is also used internally by for example the Fuel Sessions class.

It uses the encryption and hashing methods provided by PHPSecLib, so it's not dependent on external modules being available, such as mcrypt.

Advanced usage

The Crypt class supports the use of multiple instances. This might prove useful if you require multiple encryption key sets.

You can use the same methods on an crypt instance as you would when you the static methods. For the method definition see the class usage page.

forge($config = array())

The forge method allows you to manually instantiate a crypt instance.

Static Yes
Parameters
Param Default Description
$name
null
Name of the instance requested. If it doesn't exist, it will be forged. If no name is given, the default instance is returned.
$config
array()
You can pass a custom configuration when forging a new crypt instance. The configuration array has the same structure as the crypt configuration file. The custom config will be merged with a default configuration as documented here and with the configuration in your crypt config file, so you only have to pass values you want changed.
Returns object - The instantiated crypt object.
Example
// instantiate a crypt object with a custom key value
$crypt = \Crypt::forge(array(
	'crypto_key' => 'kj$Huhliuh7p*UHHhdas6%#@',
));

// and encode some data with it
$encoded = $crypt->encode('This is a very secure piece of data!');

instance($name = null)

The instance method allows you load a named instance of the crypt class.

Static Yes
Parameters
Param Default Description
$name
null
Name of the instance requested. If it doesn't exist, false is returned. If no name is given, the default instance is returned.
Returns mixed - The named crypt instance or false if the instance does not exist.
Example
// instantiate a named crypt object with a custom key
$crypt = \Crypt::instance('custom', array('crypto_key' => 'kj$Huhliuh7p*UHHhdas6%#@'));

// and encode some data with it
$encoded = $crypt->encode('This is a very secure piece of data!');

The static methods of the Crypt class will also use the default instance. Remember this if you alter the configuration of the default instance!