### Install Encrypt Bundle via Composer Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Install the latest development version of the bundle using Composer. Ensure Composer is installed globally. ```bash $ composer require specshaper/encrypt-bundle dev-master ``` -------------------------------- ### Getter for Boolean Property Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Example getter for a boolean property, ensuring a boolean return type. This can be a direct return or a ternary conversion. ```php isSelfEmployed; } /** * Get isSelfEmployed * * @return boolean */ public function isSelfEmployed(): bool { return ($this->isSelfEmployed == 1 ? true: false); } ``` -------------------------------- ### Enable SpecShaper Encrypt Bundle Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Enable the bundle by adding its class to the `config/bundles.php` file in your Symfony project. ```php ['all' => true], ]; ``` -------------------------------- ### Inject EncryptorInterface in Constructor Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Inject the EncryptorInterface service into your class constructor for easy access to encryption and decryption methods. ```php encryptor = $encryptor; } ``` -------------------------------- ### Encrypt Bundle Configuration Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Configure the encrypt bundle in `config/packages/spec_shaper_encrypt.yaml`, including the encryption key, disabling encryption, defining connections, and specifying custom listener or encryptor classes. ```yaml # app/config/packages/spec_shaper_encrypt.yaml spec_shaper_encrypt: encrypt_key: '%env(SPEC_SHAPER_ENCRYPT_KEY)%' is_disabled: false # Turn this to true to disable the encryption. connections: # Optional, define the connection name(s) for the listener - 'default' - 'tenant' listener_class: App\EventListener\MyCustomListener # Optional to override the bundle Doctrine event listener. encryptor_class: App\Encryptors\MyCustomEncryptor # Optional to override the bundle OpenSslEncryptor. annotation_classes: # Optional to override the default annotation/Attribute object. - App\Annotation\MyAttribute ``` -------------------------------- ### Generate Encryption Key Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Generate a 256-bit encryption key using the provided console command. ```bash $ bin/console encrypt:genkey ``` -------------------------------- ### Import Encrypted Annotation Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Import the Encrypted annotation class at the beginning of your entity file. ```php encrypt-bundle ### SPEC_SHAPER_ENCRYPT_KEY= change_me! ###< encrypt-bundle ### ``` -------------------------------- ### Decrypt Value using EncryptorInterface in Controller Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Demonstrates how to decrypt a value using the injected EncryptorInterface service within a controller action. ```php // Inject the Encryptor in controller actions. public function editAction(EncryptorInterface $encryptor) { ... // An example encrypted value, you would get this from your database query. $encryptedValue = "3DDOXwqZAEEDPJDK8/LI4wDsftqaNCN2kkyt8+QWr8E="; $decrypted = $encryptor->decrypt($encryptedValue); ... } ``` -------------------------------- ### Dispatch EncryptEvent for Decryption Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Shows how to dispatch an EncryptEvent to decrypt a value using the event dispatcher, useful when direct service injection is not preferred. ```php "); $dispatcher->dispatch(EncryptEvents::DECRYPT, $event); $decrypted = $event->getValue(); } ``` -------------------------------- ### Encryptor Factory Service Definition (Before) Source: https://github.com/mogilvie/encryptbundle/blob/master/CHANGELOG.md This YAML configuration shows the previous service definition for the EncryptorFactory, including logger and event dispatcher arguments. ```yaml # Factory to create the encryptor/decryptor SpecShaper\EncryptBundle\Encryptors\EncryptorFactory: arguments: ['@logger', '@event_dispatcher'] tags: - { name: monolog.logger, channel: app } SpecShaper\EncryptBundle\Encryptors\EncryptorInterface: factory: ['@SpecShaper\EncryptBundle\Encryptors\EncryptorFactory','createService'] arguments: - '%spec_shaper_encrypt.method%' - '%spec_shaper_encrypt.encrypt_key%' ``` -------------------------------- ### Encrypt/Decrypt Database Command Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Command to encrypt or decrypt the entire database. Requires specifying 'decrypt' or 'encrypt' as the action and optionally a connection name. ```bash $ bin/console encrypt:database decrypt connection ``` -------------------------------- ### Generate Encryption Key Command Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Command to generate a new encryption key for the EncryptBundle. This key is essential for the encryption and decryption process. ```bash $ bin/console encrypt:genkey ``` -------------------------------- ### Encryptor Factory Service Definition (After) Source: https://github.com/mogilvie/encryptbundle/blob/master/CHANGELOG.md This YAML configuration shows the updated service definition for the EncryptorFactory, with changes for Symfony 6 and PHP 8 compatibility, removing the logger argument and adding optional encryptor class argument. ```yaml # Factory to create the encryptor/decryptor SpecShaper\EncryptBundle\Encryptors\EncryptorFactory: arguments: ['@event_dispatcher'] tags: - { name: monolog.logger, channel: app } # The encryptor service created by the factory according to the passed method and using the encrypt_key SpecShaper\EncryptBundle\Encryptors\EncryptorInterface: factory: ['@SpecShaper\EncryptBundle\Encryptors\EncryptorFactory','createService'] arguments: $encryptKey: '%spec_shaper_encrypt.encrypt_key%' $encryptorClass: '%spec_shaper_encrypt.encryptor_class%' #optional ``` -------------------------------- ### Annotate Entity Properties for Encryption Source: https://github.com/mogilvie/encryptbundle/blob/master/README.md Apply the #[Encrypted] attribute to entity properties that require encryption. Ensure the column type is set to 'string'. Legacy '@Encrypted' annotation is supported but deprecated. ```php