You need Symfony 2.7+.
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require picodexter/parameter-encryption-bundle "~1"
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php
file of your
project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Picodexter\ParameterEncryptionBundle\PcdxParameterEncryptionBundle(),
);
// ...
}
// ...
}
Example:
In this example we will be looking at using the Caesar cipher as the encryption algorithm (which comes by default with this bundle), specifically with a rotation of 13 (ROT13).
As this type of encryption does not use any key, supplying one is pointless, however it is still shown in order to convey the main concept.
Application configuration:
.. configuration-block:: .. code-block:: yaml # app/config/config.yml pcdx_parameter_encryption: algorithms: - id: 'caesar_rot13' pattern: type: 'value_prefix' arguments: - '=#!PPE!c:r13!#=' encryption: service: 'pcdx_parameter_encryption.encryption.encrypter.caesar.rot13' key: '%parameter_encryption.caesar.rot13.key%' decryption: service: 'pcdx_parameter_encryption.encryption.decrypter.caesar.rot13' key: '%parameter_encryption.caesar.rot13.key%' .. code-block:: xml <!-- app/config/config.xml --> <?xml version="1.0" encoding="UTF-8" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ppe="https://picodexter.io/schema/dic/pcdx_parameter_encryption" xsi:schemaLocation="https://picodexter.io/schema/dic/pcdx_parameter_encryption https://picodexter.io/schema/dic/pcdx_parameter_encryption/pcdx_parameter_encryption-1.0.xsd"> <ppe:config> <ppe:algorithm id="caesar_rot13"> <ppe:pattern type="value_prefix"> <ppe:argument>=#!PPE!c:r13!#=</ppe:argument> </ppe:pattern> <ppe:encryption service="pcdx_parameter_encryption.encryption.encrypter.caesar.rot13"> <ppe:key>%parameter_encryption.caesar.rot13.key%</ppe:key> </ppe:encryption> <ppe:decryption service="pcdx_parameter_encryption.encryption.decrypter.caesar.rot13"> <ppe:key>%parameter_encryption.caesar.rot13.key%</ppe:key> </ppe:decryption> </ppe:algorithm> </ppe:config> </container> .. code-block:: php // app/config/config.php $container->loadFromExtension( 'pcdx_parameter_encryption', [ 'algorithms' => [ [ 'id' => 'caesar_rot13', 'pattern' => [ 'type' => 'value_prefix' 'arguments' => ['=#!PPE!c:r13!#='], ], 'encryption' => [ 'service' => 'pcdx_parameter_encryption.encryption.encrypter.caesar.rot13', 'key' => '%parameter_encryption.caesar.rot13.key%', ], 'decryption' => [ 'service' => 'pcdx_parameter_encryption.encryption.decrypter.caesar.rot13', 'key' => '%parameter_encryption.caesar.rot13.key%', ], ], ], ] );
Parameters:
.. configuration-block:: .. code-block:: yaml # app/config/parameters.yml parameters: parameter_encryption.caesar.rot13.key: 'not necessary for the Caesar cipher' .. code-block:: xml <!-- app/config/parameters.xml --> <?xml version="1.0" encoding="UTF-8" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <parameters> <parameter key="parameter_encryption.caesar.rot13.key">not necessary for the Caesar cipher</parameter> </parameters> </container> .. code-block:: php // app/config/parameters.php $container->setParameter('parameter_encryption.caesar.rot13.key', 'not necessary for the Caesar cipher');
Generate an encrypted value via the encrypt CLI command (see :doc:`/cli`).
Save the value as a parameter with the configured prefix.
Example (based on example configuration shown above):
- Plaintext value:
This is a test
- Encrypted value:
Guvf vf n grfg
- Save as a parameter with the value:
=#!PPE!c:r13!#=Guvf vf n grfg
- Plaintext value:
Read or use the parameter as you would normally.
There are currently two ways to get other encryption algorithms (apart from the one contained in this bundle) to work:
Get and install an add-on bundle.
Code your own.
Implement the :class:`Picodexter\ParameterEncryptionBundle\Encryption\Decrypter\DecrypterInterface` and the :class:`Picodexter\ParameterEncryptionBundle\Encryption\Encrypter\EncrypterInterface` respectively.
Add Symfony services for the newly implemented classes.
Add an algorithm entry in the bundle configuration.