tobento / service-encryption
PHP 应用程序的加密。
1.0.1
2023-06-11 09:03 UTC
Requires
- php: >=8.0
- defuse/php-encryption: ^2.3
- psr/container: ^2.0
- tobento/service-autowire: ^1.0.9
Requires (Dev)
- phpunit/phpunit: ^9.5
- tobento/service-container: ^1.0.6
- vimeo/psalm: ^4.0
README
使用 Crypto 作为默认实现的 PHP 应用程序加密接口。
目录
入门
使用以下命令添加正在运行的加密服务项目的最新版本。
composer require tobento/service-encryption
要求
- PHP 8.0 或更高版本
亮点
- 框架无关,与任何项目兼容
- 解耦设计
文档
基本用法
加密和解密
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.
查看 Encrypters Interface 了解更多信息。
查看 Encrypter Interface 了解更多信息。
懒加密器
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.
查看 Encrypters Interface 了解更多信息。
查看 Encrypter Interface 了解更多信息。
加密实现
您可以查看 Crypto 了解更多信息。
用法
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)