PHP code example of tobento / service-encryption

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


use Tobento\Service\Encryption\EncrypterInterface;

class SomeService
{
    public function __construct(
        private EncrypterInterface $encrypter,
    ) {
        $encrypted = $encrypter->encrypt('something');
        
        $decrypted = $encrypter->decrypt($encrypted);
    }
}

namespace Tobento\Service\Encryption;

interface EncrypterFactoryInterface
{
    /**
     * Create a new Encrypter.
     *
     * @param string $name
     * @param array $config
     * @return EncrypterInterface
     * @throws EncrypterException
     */
    public function createEncrypter(string $name, array $config): EncrypterInterface;
}

namespace Tobento\Service\Encryption;

interface EncrypterInterface
{
    /**
     * Returns the encrypter name.
     *
     * @return string
     */
    public function name(): string;
    
    /**
     * Returns the encrypted data.
     *
     * @param mixed $data
     * @return string
     * @throws EncryptException
     */
    public function encrypt(mixed $data): string;
    
    /**
     * Returns the decrypted data.
     *
     * @param string $encrypted
     * @return mixed
     * @throws DecryptException
     */
    public function decrypt(string $encrypted): mixed;
}

namespace Tobento\Service\Encryption;

interface EncryptersInterface
{
    /**
     * Returns true if encrypter exists, otherwise false.
     *
     * @param string $name
     * @return bool
     */
    public function has(string $name): bool;
    
    /**
     * Returns the encrypter if exists, otherwise null.
     *
     * @param string $name
     * @return null|EncrypterInterface
     */
    public function get(string $name): null|EncrypterInterface;
}

namespace Tobento\Service\Encryption;

interface KeyGeneratorInterface
{
    /**
     * Returns a generated new key.
     *
     * @return string
     * @throws KeyException
     */
    public function generateKey(): string;
}

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

$encrypters = new Encrypters(
    $encrypter, // EncrypterInterface
    $anotherEncrypter, // EncrypterInterface
);

var_dump($encrypters instanceof EncryptersInterface);
// bool(true)

var_dump($encrypters instanceof EncrypterInterface);
// bool(true)
// Uses the first encrypter specified.

use Tobento\Service\Encryption\LazyEncrypters;
use Tobento\Service\Encryption\EncryptersInterface;
use Tobento\Service\Encryption\EncrypterInterface;
use Tobento\Service\Encryption\EncrypterFactoryInterface;
use Tobento\Service\Encryption\Crypto;
use Psr\Container\ContainerInterface;

$encrypters = new LazyEncrypters(
    container: $container, // ContainerInterface
    encrypters: [
        'default' => [
            // factory must implement EncrypterFactoryInterface
            'factory' => Crypto\EncrypterFactory::class,
            'config' => [
                'key' => 'secret-key',
            ],
        ],
        
        'cookies' => [
            // ...
        ],
    ],
);

var_dump($encrypters instanceof EncryptersInterface);
// bool(true)

var_dump($encrypters instanceof EncrypterInterface);
// bool(true)
// Uses the first encrypter specified.

use Tobento\Service\Encryption\Crypto\KeyGenerator;
use Tobento\Service\Encryption\Crypto\EncrypterFactory;
use Tobento\Service\Encryption\KeyGeneratorInterface;
use Tobento\Service\Encryption\EncrypterFactoryInterface;
use Tobento\Service\Encryption\EncrypterInterface;

$keyGenerator = new KeyGenerator();

var_dump($keyGenerator instanceof KeyGeneratorInterface);
// bool(true)

// Generate a key and store it savely for reusage.
$key = $keyGenerator->generateKey();

$encrypterFactory = new EncrypterFactory();

var_dump($encrypterFactory instanceof EncrypterFactoryInterface);
// bool(true)

$encrypter = $encrypterFactory->createEncrypter(
    name: 'crypto',
    config: ['key' => $key],
);

var_dump($encrypter instanceof EncrypterInterface);
// bool(true)