ionghitun/laravel-lumen-mysql-encryption

该软件包已被弃用且不再维护。未建议替换软件包。

适用于mysql数据库的laravel和lumen的数据库字段加密,具有本地搜索和匿名化数据以符合gdpr。

4.5.0 2022-08-01 18:39 UTC

This package is auto-updated.

Last update: 2023-03-30 19:14:18 UTC


README

Latest Stable Version Build Status Total Downloads Scrutinizer Code Quality License

Laravel Mysql Encryption

为mysql数据库提供本地搜索和匿名化数据的laravel和lumen的数据库字段加密。

安装说明

$ composer require ionghitun/laravel-lumen-mysql-encryption

依赖项

  • php >= 8.0

文档

服务提供程序将自动为laravel加载,对于lumen,您需要在您的bootstrap/app.php文件中添加以下内容:

$app->register(IonGhitun\MysqlEncryption\MysqlEncryptionServiceProvider::class);

到您的 bootstrap/app.php 文件。

您需要在.env文件中添加ENCRYPTION_KEY,长度必须是16个字符。

每个您的模型都应该扩展IonGhitun\MysqlEncryption\Models\BaseModel,并且对于您希望加密的字段,您必须执行以下操作

  • 在迁移中设置字段为binary

  • 向您的模型添加$encrypted

      /** @var array */
      protected $encrypted = [
          'name',
          'email'
      ];
    

您可以使用Validator对以下字段进行验证

  • unique_encrypted

      unique_encrypted:<table>,<field(optional)>
    
  • exists_encrypted

      exists_encrypted:<table>,<field(optional)>
    

您不能在加密字段上使用基本的where,orWhere,orderBy,因此有5个预定义的作用域可供使用,作为替代

  - whereEncrypted
  - whereNotEncrypted
  - orWhereEncrypted
  - orWhereNotEncrypted
  - orderByEncrypted

匿名化数据的功能

  • 在您的模型上设置$anonymizable变量,数据将使用https://github.com/fzaninotto/Faker进行匿名化,有关所有可用类型的详细信息,请查看此软件包。

示例

    //without extra parameters needed for randomDigit
    protected $anonymizable = [
        'age' => ['randomDigit']
    ];
    
    //with extra parameters needed for numberBetween
    protected $anonymizable = [
        'age' => ['numberBetween', '18','50']
    ];
  • 获取您的模型实例并使用匿名化方法:$user->anonymize();

该方法接受一个区域设置参数,如果您想使用具有区域设置的faker,默认区域设置可以在.env文件中设置:FAKER_LOCALE = 'en_US'

如果上述任何方法未指定,则默认Faker区域设置将被使用

注意:模型不会自动保存!

祝您编码愉快!

为了撤销此软件包加密的数据,您需要从$encrypted数组中删除列,遍历您的模型,并将$aesDecrypt($value)保存到所有在$encrypted数组中的列,然后更改列从binary到更合适的数据类型。

在此之后,您可以删除该软件包并扩展基本laravel模型,然后进行清理,删除与此软件包相关的所有内容!