gracious/doctrine-encryption-bundle

为 Doctrine 添加加密类型的简单包

4.0.0 2024-03-06 10:50 UTC

This package is auto-updated.

Last update: 2024-09-06 12:25:18 UTC


README

简单包,用于向 Doctrine 添加 2 种新类型

  • encrypted
  • encryptedArrayCollection
  • hashed

它依赖于 libSodium 进行加密

安装

安装相当简单

  1. 通过 composer 需求此包
composer require gracious/doctrine-encryption-bundle
  1. 将以下内容添加到您的 doctrine.yaml 文件中
types:
  encrypted: 'Gracious\DoctrineEncryptionBundle\Type\Encrypted'
  encryptedArrayCollection: 'Gracious\DoctrineEncryptionBundle\Type\EncryptedArrayCollection'
  hashed: 'Gracious\DoctrineEncryptionBundle\Type\Hashed'
  1. 生成一个 64 位的加密密钥,您可以通过以下方式完成
sodium_bin2hex(random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES));
  1. 将以下两个设置添加到您的 .env 文件中
ENABLE_ENCRYPTION=true
ENCRYPTION_KEY=[PASTE ENCRYPTION KEY HERE]

设置

目前有 2 个设置,都是环境变量

  • ENABLE_ENCRYPTION - true / false

  • ENCRYPTION_KEY - 64 位十六进制字符串

生成密钥

您可以通过两种方式生成密钥,要么自己输入,要么运行

sodium_bin2hex(random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES));

随机数

随机数会自动为每个加密值生成,并按照以下方式添加到返回值中

<nonce|encrypted value>

Doctrine 设置

以下内容必须添加到您的 doctrine.yaml 中

types:
  encrypted: 'Gracious\DoctrineEncryptionBundle\Type\Encrypted'
  encryptedArrayCollection: 'Gracious\DoctrineEncryptionBundle\Type\EncryptedArrayCollection'
  hashed: 'Gracious\DoctrineEncryptionBundle\Type\Hashed'

该块可能看起来像这样

doctrine:
    dbal:
        # configure these for your database server
        driver: 'pdo_mysql'
        server_version: '5.7'
        charset: utf8mb4
        default_table_options:
            charset: utf8mb4
            collate: utf8mb4_unicode_ci

        # With Symfony 3.3, remove the `resolve:` prefix
        url: '%env(resolve:DATABASE_URL)%'
        types:
          encrypted: 'Gracious\DoctrineEncryptionBundle\Type\Encrypted'
          encryptedArrayCollection: 'Gracious\DoctrineEncryptionBundle\Type\EncryptedArrayCollection'
          hashed: 'Gracious\DoctrineEncryptionBundle\Type\Hashed'

使用方法

要在您的实体中使用这 3 种类型中的任何一种,只需将列类型替换为

@ORM\Column(type="encrypted")

@ORM\Column(type="encryptedArrayCollection")

@ORM\Column(type="hashed")