PHP code example of tobento / app-encryption

1. Go to this page and download the library: Download tobento/app-encryption 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/ */

    

tobento / app-encryption example snippets


use Tobento\App\AppFactory;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Encryption\Boot\Encryption::class);

// Run the app:
$app->run();

return [
    
    //...
    
    /*
    |--------------------------------------------------------------------------
    | Encrypters
    |--------------------------------------------------------------------------
    |
    | Specify the encrypters for your application.
    |
    */
    
    'encrypters' => [
        
        'default' => [
            // must implement EncrypterFactoryInterface
            'factory' => Crypto\EncrypterFactory::class,
            
            'config' => [
                // replace existing key:
                'key' => '{default-encrypt-key}',
            ],
            
            // must implement KeyGeneratorInterface
            'keyGenerator' => Crypto\KeyGenerator::class,
        ],

    ],
];

use Tobento\App\AppFactory;
use Tobento\Service\Encryption\EncryptersInterface;
use Tobento\Service\Encryption\EncrypterInterface;

// Create the app
$app = (new AppFactory())->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots:
$app->boot(\Tobento\App\Encryption\Boot\Encryption::class);
$app->booting();

// using the default encrypter:
$encrypter = $app->get(EncrypterInterface::class);

// using a specified encrypter:
$encrypters = $app->get(EncryptersInterface::class);

$cookieEncrypter = $encrypters->get('cookies');

// you may check if the encrypter exists:
var_dump($encrypters->has('cookies'));
// bool(true)

// encrypt and decrypt:
$encrypted = $encrypter->encrypt('something');
        
$decrypted = $encrypter->decrypt($encrypted);

// Run the app:
$app->run();

use Tobento\Service\Encryption\EncryptersInterface;
use Tobento\Service\Encryption\EncrypterInterface;

class SomeService
{
    public function __construct(
        protected EncryptersInterface $encrypters,
        protected EncrypterInterface $encrypter,
    ) {}
}

use Tobento\App\Boot;
use Tobento\App\Encryption\Boot\Encryption;

class AnyServiceBoot extends Boot
{
    public const BOOT = [
        // you may ensure the encryption boot.
        Encryption::class,
    ];
    
    public function boot(Encryption $encryption)
    {
        $encrypter = $encryption->encrypter();
        $encrypters = $encryption->encrypters();
    }
}
app/config/encryption.php
app/config/encryption.php