socfest / encrypted-types
1.1
2021-02-23 12:18 UTC
Requires
- php: ^7.1.3|^8.0
- ext-openssl: *
- symfony/config: ^3.4 || ^4.0 || ^5.0
- symfony/dependency-injection: ^3.4 || ^4.0 || ^5.0
- symfony/http-kernel: ^3.4 || ^4.0 || ^5.0
Requires (Dev)
- phpunit/phpunit: ^8.0
Suggests
- doctrine/doctrine-bundle: For use with Doctrine ORM
- doctrine/mongodb-odm-bundle: For use with Doctrine ODM
README
存储加密的数据库数据
加密方法
- base64
- openssl
安装
通过composer安装包
composer require socfest/encrypted-types
在bundles.php中初始化bundle
# config/bundles.php
return [
// for ODM
Socfest\MongoDB\SocfestMongoEncryptedTypesBundle::class => ['all' => true],
//for ORM
Socfest\ORM\SocfestORMEncryptedTypesBundle::class => ['all' => true],
]
您可以通过在.env文件中输入以下内容来指定openssl加密方法。
请参阅php.net openssl_cipher_iv_length 页面 (https://php.ac.cn/manual/en/function.openssl-cipher-iv-length.php)
OPENSSL_CIPHER=%cipher_method%
用法
ODM
在模型中,您可以指定字段类型。新的加密类型有
- encrypted_openssl
- encrypted_base64
/**
* @var string
* @MongoDB\Field(type="encrypted_openssl")
*/
protected $fullName;
在查询中,您可以通过完整值找到,您必须在设置等于之前进行加密。
不要忘记使用正确的加密器!
$this->dm->getRepository(User::class)
->createQueryBuilder()
->field('fullName')
->equals(OpenSSLEncrypter::encrypt('X Y'))
->getQuery()
->execure()
);
也适用于find方法
$this->dm->getRepository(User::class)
->findOneBy([
'fullName' => OpenSSLEncrypter::encrypt('X Y')
])