tobento / app-encryption
应用加密支持。
1.0.1
2024-02-20 17:57 UTC
Requires
- php: >=8.0
- tobento/app: ^1.0
- tobento/app-migration: ^1.0
- tobento/service-config: ^1.0.4
- tobento/service-encryption: ^1.0.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- tobento/service-filesystem: ^1.0
- vimeo/psalm: ^4.0
README
应用加密支持。
目录
入门指南
使用以下命令添加运行中的应用加密项目最新版本。
composer require tobento/app-encryption
要求
- PHP 8.0 或更高版本
文档
应用
如果您使用的是骨架,请查看 应用骨架。
您还可以查看 应用 以了解更多关于应用的一般信息。
加密启动
加密启动执行以下操作
- 安装和加载加密配置文件
- 为配置文件(重新)生成加密密钥
- 根据加密配置实现加密接口
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();
您可以通过查看 加密服务 来了解更多。
加密配置
加密配置位于默认应用骨架配置位置下的 app/config/encryption.php
文件,您可以在其中指定应用的加密器。
重新生成加密密钥
如果您想重新生成加密密钥,只需在 app/config/encryption.php
文件中将现有密钥替换为 {任何唯一名称}
。在下一次启动时,它将自动重新生成。
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();
使用自动注入
您还可以在任何由应用解析的类中请求 EncryptersInterface::class
或 EncrypterInterface::class
。
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(); } }
您可以通过查看 加密服务 来了解更多。