PHP code example of psolutions / encrypt-bundle

1. Go to this page and download the library: Download psolutions/encrypt-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

psolutions / encrypt-bundle example snippets




return [
    ...
    PSolutions\EncryptBundle\PSolutionsEncryptBundle::class => ['all' => true],
];



...
use PSolutions\EncryptBundle\Annotations\Encrypted;



    #[Encrypted]
    #[Column]
    protected string $taxNumber;

    #[Column(type: string, nullable: true)]
    #[Encrypted]
    protected ?bool $isSelfEmployed;

    /**
     * Date of birth
     */
    #[Encrypted]
    #[Column]
    protected ?String $dob;



    /**
     * Get isSelfEmployed
     *
     * @return boolean
     */
    public function isSelfEmployed(): bool
    {
        return $this->isSelfEmployed;
    }

    /**
     * Get isSelfEmployed
     *
     * @return boolean
     */
    public function isSelfEmployed(): bool
    {
        return ($this->isSelfEmployed == 1 ? true: false);
    }



    use PSolutions\EncryptBundle\Encryptors\EncryptorInterface;

    // Inject the Encryptor from the service container at class construction
    public function __construct(private readonly EncryptorInterface $encryptor)
    {

    }

    // 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=<ENC>";

        $decrypted = $encryptor->decrypt($encryptedValue);
        ...
    }




    ...
    use PSolutions\EncryptBundle\Event\EncryptEvent;
    use PSolutions\EncryptBundle\Event\EncryptEvents;
    use Symfony\Component\EventDispatcher\EventDispatcherInterface;
    ...

    public function indexAction(EventDispatcherInterface $dispatcher)
    {
        ...
        // An example encrypted value, you would get this from your database query.
        $event = new EncryptEvent("3DDOXwqZAEEDPJDK8/LI4wDsftqaNCN2kkyt8+QWr8E=<ENC>");

        $dispatcher->dispatch(EncryptEvents::DECRYPT, $event);

        $decrypted = $event->getValue();
    }

use PSolutions\EncryptBundle\Encryptors\EncryptorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class MyCustomEncryptor implements EncryptorInterface
{
    private EventDispatcherInterface $dispatcher;
    private string $secretKey;

    public function __construct(EventDispatcherInterface $dispatcher)
    {
        $this->dispatcher = $dispatcher;
    }

    public function setSecretKey(string $key): void
    {
        $this->secretKey = $key;
    }

    public function encrypt(?string $data): ?string
    {
        // Your custom encryption logic
        // Must return null for null input
        // Must check for '<ENC>' suffix and return if present
    }

    public function decrypt(?string $data): ?string
    {
        // Your custom decryption logic
        // Must return null for null input
        // Must check for '<ENC>' suffix and return if not present
    }
}
bash
   php bin/console encrypt:database encrypt
   
bash
php bin/console encrypt:database decrypt
bash
php bin/console encrypt:database encrypt