xorgxx/doctrine-encryptor-bundle

此捆绑包为您在应用程序的Db系统中加密/解密敏感数据。其主要目标是使您能够轻松管理加密和解密到Db中的敏感数据!此捆绑包旨在更新旧的捆绑包 [DoctrineEncryptBundle]

0.1.0 2024-03-22 12:59 UTC

This package is auto-updated.

Last update: 2024-09-11 10:33:09 UTC


README

DoctrineEncryptorBundle { Symfony 6/7 }

此捆绑包为您在应用程序的Db系统中加密/解密敏感数据。其主要目标是使您能够轻松管理加密和解密到Db中的敏感数据!此捆绑包旨在更新旧的捆绑包 DoctrineEncryptBundle

此捆绑包的目标是自动且透明地建立一个强大的加密和解密系统,通过外部化数据,严格遵守欧洲建议和通用数据保护条例(GDPR)的指导方针。

PSR-12

Doctrineencryptor-schema.png

cryptor.png

安装 RELEASE !

使用Composer安装此捆绑包

  composer require xorgxx/doctrine-encryptor-bundle:^0.1 or dev-master

Doctrine迁移

🚨 您将需要迁移以添加NeoxEncryptor到您的实体中。 🚨

  symfony make:migration
  symfony doctrine:migrations:migrate

安装、设置、文件夹、.pem .key

  • 您可能需要手动创建文件夹:config/doctrine-encryptor
  • php bin/console neox:encryptor:install 遵循说明(此命令将为您设置配置文件:doctrine-encryptor 和 gaufrette)。
  • php bin/console neox:encryptor:openssl 遵循说明。

注意: 您可能需要使用 [symfony composer dump-autoload] 重新加载自动加载

..... 完成 🎈

配置文件

doctrine_encryptor.yaml文件

  doctrine_encryptor:
    # (default)false or true | it will turn off the bundle. by aware that it will render nothing !! field on front will by empty!!
    # this is only for testing purpose in Development mode !!!
    nencryptor_off: false
    encryptor_cipher_algorithm: AES-256-CBC  # AES-256-CBC | !!! Camellia-256-CBC !!!
    encryptor_system: halite # halite | openSSLSym | !!! DEPRECIATED openSSLAsym !!! (das not support advance typing (obejt, array, ...) yet) 
    encryptor_storage: 'gaufrette:local' # name of filesystems in you config/gaufrette.yaml
    encryptor_cache: false # use true for ussing cache system. you will have to setup your application before  

我们建议设置您的缓存系统 symfony cache

更多安全性:从外部存储获取密钥

🚨 我们使用 KnpGaufrette 。在此配置中,所有密钥都存储在外部,无法从您的网站内部访问。这意味着即使有人访问了您的代码,他们也无法访问密钥,为您的加密系统提供额外的安全层。为了配置,您至少需要配置一个“适配器”:config/gaufrette.yaml

  knp_gaufrette:
      adapters:
          local_adapter:
              local:
                  directory: '%kernel.project_dir%/config/doctrine-encryptor/'
  
      filesystems:
          local:
              adapter: local_adapter
/** 
* ===== openSSLSym is match faster !! | ======
* openSSLAsym because is Asymetric we cant put macth data in encrypte SO it's not working well yet!!
* 🚨 Due to instability issues (after ~100 caractes), it is advisable not to use 
* the openSSLAsym encryptor for handling advanced data typing (obejt, array, ...).!!
**/

使用说明!

在实体中,您想要在实体中安全字段(数据)

  use DoctrineEncryptor\DoctrineEncryptorBundle\Attribute\NeoxEncryptor;
  ....
  
    #[ORM\Column(type: Types::TEXT, nullable: true)]
    #[neoxEncryptor(build: "out", facker : )]
    private ?string $content = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    #[neoxEncryptor(build: "in")]
    private ?string $description = null;
 
    
    /** =======   note that by default #[neoxEncryptor] 
    * Attribute : build: "in". be default  in / out
    * Attribute : facker: PhoneFacker::class. be default This give possibility to customize the "facker" for ex: type phoneNumber it's not buildin bundle, but hee you can make service.
    **/
 
  ....

自定义facker

这是专门用于管理类型,并希望在数据库中显示的。大多数属性都由捆绑包字符串、int、date等识别,但在某些情况下,例如PhoneNumber,[...]捆绑包将不会识别!因此,您需要添加服务。

    <?php
      namespace App\Services;
      use libphonenumber\PhoneNumber;
      class PhoneFacker implements neoxFackerInterface
      {
          public function create( ): PhoneNumber
          {
              return  (new PhoneNumber())
                  ->setCountryCode(33)
                  ->setNationalNumber("14155552671")
              ;
          }
      }

然后在实体文件中添加属性[facker: PhoneFacker::class]

    #[AssertPhoneNumber(type: [AssertPhoneNumber::MOBILE], defaultRegion: 'FR')]
    #[ORM\Column(type: "phone_number", nullable: true)]
    #[neoxEncryptor(build: "out", facker: PhoneFacker::class)]
    private ?PhoneNumber $phoneNumber = null;
    

TWIG

在模板twig中管理解密字段

  # first put entity and the field name to decrypt`
  {{ health.profile | doctrineDecrypt("firstName") }}
  

重要!

在选择“在”时,请考虑您要加密的字段的大小/长度!例如:长度:20

  #[neoxEncryptor]
  #[ORM\Column(length: 20)]
  private ?string $name = null;
  
  "john doe" <- decrypt (length:8) ✅  / ⛔ (length: +20!!) encrypt -> "MUIFAOpLp21iX1Dy2ZNkYbby6zo7ADYgVs-hGkNaWR2OF5AbQUMcBKZHigtFVxZiIFWyOTV8Ts-9q_pNAHBxCKcAPZNJjfPgVQglMLAKi0bZicmPlCQKJpRpX2k5IAjAqawOlFsPpD9KikIEFRhuy"
  

小心!

  • 无法对加密字段进行索引或搜索

🚨 🚨 危险 🚨🚨

!!! 在您更改任何密钥、属性“in”/“out”之前 !!!

  1. 解密全部
  2. 更改您想要更改的内容,例如:从“in”到“out”的属性
  3. 加密全部

[CLI] 内置命令

在加密器之间切换

首先:

  • php bin/console neox:encryptor:switch (切换到新加密器的命令,例如,从halite切换到openSSLSym)

例如 | php bin/console neox:encryptor:switch | 自动进程将执行此操作:例如,从halite切换到openSSLSym

  • 使用当前加密器halite解密所有内容
  • 修改 doctrine_encryptor.yaml |-> encryptor_system: halite >>> openSSLSym
  • 清除缓存

!!! 此过程是为了防止在更换或更新SSL密钥期间发生数据丢失。

然后您需要手动创建新的SSL密钥:

  • php bin/console neox:encryptor:openssl (创建 .pem & .key 的命令)

  • php bin/console neox:encryptor:halite (创建 .pem & .key 的命令)

最后,为了加密/解密数据:

  • php bin/console neox:encryptor:wasaaaa
  • php bin/console neox:encryptor:renew // 用于更改所有 .pem & .key 文件的命令。主要是更改加密方式。

例如 | php bin/console neox:encryptor:switch | 自动进程将执行此操作:例如,从halite切换到openSSLSym

  • 使用当前加密器halite解密所有内容
  • 修改 doctrine_encryptor.yaml |-> encryptor_system: halite >>> openSSLSym
  • 清除缓存
  • 然后您可以使用 php bin/console neox:encryptor:wasaaaa 正常加密。

!! ??? WASAAAA ? 😉

❔如果您加密或解密的时间很长,那么它将只是加密或解密很长时间。数据仍然会被管理。

工具功能

偶尔,我们可能需要访问完整范围的数据(4000行或更多)以进行各种检查或分析。然而,由于加密数据的转换而等待数小时是不理想的。在这种情况下,禁用EventListener是必要的。

  use DoctrineEncryptor\DoctrineEncryptorBundle\Pattern\NeoxDoctrineTools;  
  ...
  // this will stop eventlistener to decrypt
  $neoxDoctrineTools->EventListenerPostLoad();
  
  $entity = $parametersRepository->findAll()
  
  // this will restart eventlistener to decrypt
  $neoxDoctrineTools->EventListenerPostLoad(true);
  
  // data on the field encrypted will be empty

🚨🚨 未来版本中的功能增强 框架内的功能

贡献

如果您想为此包做出贡献(谢谢!)以下是一些指南

  • 我不是Pest/PHPUnit测试的专家,所以我没有完成测试。如果有人可以提供帮助,请与我联系。谢谢!

  • 请尊重 Symfony指南
  • 测试一切!当您
    • 修复之前未涵盖的bug
    • 添加新功能
    • 看到代码工作但未通过任何测试(在天堂有一个特殊的地方为您预留)

待办事项

  • 命令行历史记录/日志以在加密/解密时提供保护!!
  • 测试App!!需要帮助。
  • 它不转换高级类型(对象,数组,日期等)。
  • 添加远程系统存储哈希 => 密钥
  • 以便能够根据属性类型 | int,string,电话等进行加密/解密。
  • 自定义提供程序类加密/解密。
  • 分发到自定义代码。
  • 命令wasaaaa:轻松管理状态,加密,解密等。

谢谢