cleverage / encryption-bundle
EncryptionBundle 允许您以非常简单的方式在 Doctrine 的实体中存储加密的文件和数据
Requires
- php: ^7.4
- ext-mbstring: *
- doctrine/doctrine-bundle: *
- doctrine/orm: *
- php-parallel-lint/php-var-dump-check: ^0.5.0
- symfony/framework-bundle: ^4.4|^5.1
- symfony/monolog-bundle: ^3.6
- symfony/security: ^4.4|^5.1
- symfony/string: ^5.2
Requires (Dev)
- phpseclib/mcrypt_compat: ^1.0
- phpunit/phpunit: ^9.4
- symfony/stopwatch: ^4.4|^5.1
Suggests
- ext-mcrypt: Don't use this except for backward compatibility purpose
- ext-sodium: The Sodium library is native in PHP 7.2
- paragonie/sodium_compat: Use this for a Sodium polyfill
- phpseclib/mcrypt_compat: If you need backward compatibility but don't want to compile the extension
Conflicts
README
为 Symfony 提供简单易用的实体和文件加密。
我们希望能够在我们的 Symfony2 应用程序中存储加密数据,但发现没有简单的方法可以实现。
在我们的解决方案中,恶意用户将很难从我们的服务器中窃取数据。
- 数据不会被 SQL 注入或远程包含攻击所损害。
- 如果没有终端用户连接,没有人可以解密数据,即使是 root 用户也不行。
- 只有来自同一组织的用户可以相互之间共享数据。
想法是将加密密钥存储在数据库的用户表中,但使用用户的明文密码进行加密。这样,来自同一组织的每个用户都可以共享相同的加密密钥来加密和解密数据,但每个用户只能在登录时解密自己的加密密钥。
该系统的主要弱点是加密密钥暂时存储在 PHP 的会话中,然而,要解决这个问题,唯一的方法是在客户端和服务器之间使用相当复杂的非对称加密系统,这只能在富客户端上正确实现。
安装
composer require cleverage/encryption-bundle
用法
use Doctrine\ORM\Mapping as ORM; class MyEntity { /** * @ORM\Column(type="encrypt_string") */ private string $myField; /** * @ORM\Column(type="encrypt_text") */ private string $myFieldText; }
辅助工具
此捆绑包附带一个 EncryptionManager 类,可用于独立加密和解密数据和文件。还有一个 DecryptFileResponse,允许您在解密的同时直接将加密文件流式传输到客户端。
安装
您可以直接在 composer.json 中或通过命令行要求包 sidus/encryption-bundle
$ composer require sidus/encryption-bundle ~0.1.0
更新 AppKernel.php
public function registerBundles()
{
$bundles = array(
...
new Sidus\EncryptionBundle\SidusEncryptionBundle(),
);
...
}
实现接口
您应该在用户实体上实现 UserEncryptionProviderInterface,在每个将包含加密数据的实体上实现 CryptableInterface。
别忘了更新模型,加密密钥必须持久化到数据库中!
配置
如果您需要在用户之间共享加密数据,则需要使用相同的密钥生成每个加密密钥,这可能相当棘手,尤其是如果用户已经有了账户和密码。
然而,如果每个用户加密自己的数据,则可以在 config.yml 中使用自动加密密钥生成。
sidus_encryption: encryption_key: auto_generate: false throw_exceptions: true # Do not throw an exception when an error occurred when decrypting a value
这将告诉系统如果用户没有密钥,将自动生成新的加密密钥。
在密码恢复的情况下,用户将无法恢复任何加密数据,因为他将是唯一能够解密密钥的人。
禁用加密
可以在加密类型(例如命令)上临时禁用加密。这可以通过使用 Sidus\EncryptionBundle\Encryption\Enabler\EncryptionEnablerInterface
服务来实现。
// ... $encryptionEnabler->disableEncryption(); // Starting from here, data will not be decrypted $encryptionManager->decryptString($value); // The value will not be decrypted $encryptionManager->encryptString($value); // The value will not be encrypted and store as is $encryptionEnabler->enableEncryption(); // Now the encryption is re-enabled and works normally
Apache 许可证
@待办事项
作者
此捆绑包最初由 Vincent Chalnot 创建。