psamatt / mcrypt-service-provider
A simple wrapper of the mcrypt library for use within Silex
Requires
- php: >=5.3.2
- ext-mcrypt: *
- silex/silex: 1.*
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-12-30 16:21:28 UTC
README
A simple wrapper of the PHP Mcrypt library for Silex
Usage
Register the service provider and specify your unique key.
$app->register(new Psamatt\Silex\Provider\McryptServiceProvider('unique_key', array( 'cipher' => MCRYPT_RIJNDAEL_256, // optional 'mode' => MCRYPT_MODE_CBC, // optional 'iv_source' => MCRYPT_DEV_RANDOM, // optional 'base64' => true|false, // optional. Default is true 'auto_generate_iv' => true|false, // option. Default is false )));
Please note that you must explicitly generate the IV if you leave auto_generate_iv
to false
, you can do this by the following:
$app['mcrypt']->generateIv();
In your Silex application, you can use the Mcrypt provider with the following lines:
$data = 'my string'; $encryptedKey = $app['mcrypt']->encrypt($data); print $app['mcrypt']->decrypt($encryptedKey); // prints 'my string'
If you'd like to use mcrypt in your Twig templates*, you can using either the mcrypt_encrypt
or the mcrypt_decrypt
filter:
{{ object.method | mcrypt_encrypt }} // encrypt {{ object.method | mcrypt_decrypt }} // decrypt
* ensure you define the McryptServiceProvider
after your Twig Service Provider to utilise the Twig feature
Other useful information
If you require to decrypt a mcrypt encrypted string and are not using MCRYPT_MODE_ECB (recommended), you must initially get the IV before you've encrypted your string using $app['mcrypt']->getIv()
and store this safely, then when you are ready to decrypt, you must set the same IV using $app['mcrypt']->setIv($my_iv)
, view the unit test for further clarification.
Mcrypt Documentation
For more information on what values to use for Mcrypt, view the documentation on each specific type:
By default, the encrypted string will use base 64 encoding and decoding, to set this for your application, set the base64 option to true
or false
.