tobento/service-encryption

Encryption for PHP applications.

1.0.1 2023-06-11 09:03 UTC

This package is auto-updated.

Last update: 2024-04-11 10:57:59 UTC


README

Encryption interfaces for PHP applications using Crypto as default implementation.

Table of Contents

Getting started

Add the latest version of the encryption service project running this command.

composer require tobento/service-encryption

Requirements

  • PHP 8.0 or greater

Highlights

  • Framework-agnostic, will work with any project
  • Decoupled design

Documentation

Basic Usage

Encrypt And Decrypt

use Tobento\Service\Encryption\EncrypterInterface;

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

Interfaces

Encrypter Factory Interface

You may use the encrypter factory interface for creating encrypters.

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;
}

Encrypter Interface

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;
}

Encrypters Interface

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;
}

Key Generator Interface

You may use the key generator interface for generating keys.

namespace Tobento\Service\Encryption;

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

Encrypters

You may use the following encrypters if you want to manage multiple encrypters for your application.

Default Encrypters

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.

Check out the Encrypters Interface to learn more about it.

Check out the Encrypter Interface to learn more about it.

Lazy Encrypters

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.

Check out the Encrypters Interface to learn more about it.

Check out the Encrypter Interface to learn more about it.

Crypto Implementation

You may check out the Crypto to learn more about it.

Usage

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)

Credits