本文介绍了在哪里加密/解密我的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony 2与Doctrine 2。



我需要使用加密服务加密我的实体中的一个字段,我想知道我应该在哪里这个逻辑。



我正在使用Controller> Service> Repository架构。



我想知道一个听众将是一个好主意,我的主要关注是,如果我的实体被加密存储,如果我在它的状态下解密它将被改变,我不知道这是一个好主意。



你将如何实现这一点?

解决方案

要在 >和 targnation 的很好的答案,一种将依赖关系(例如,加密服务)注入到自定义的Doctrine映射类型中的方法可能是使用静态属性和设置器:

  // MyBundle / Util / Crypto / Types / EncryptedString.php 
class EncryptedString extends StringType
{
/ * * @var \MyBundle\Util\Crypto * /
protected static $ crypto;

public static function setCrypto(Crypto $ crypto)
{
static :: $ crypto = $ crypto;
}

public function convertToDatabaseValue($ value,AbstractPlatform $ platform)
{
$ value = parent :: convertToDatabaseValue($ value,$ platform);
return static :: $ crypto-> encrypt($ value);
}

public function convertToPHPValue($ value,AbstractPlatform $ platform)
{
$ value = parent :: convertToPHPValue($ value,$ platform);
return static :: $ crypto-> decrypt($ value);
}

public function getName()
{
return'encrypted_string';
}
}

配置如下所示:

  // MyBundle / MyBundle.php 
class MyBundle extends Bundle
{
public function boot()
{
/ ** @var \MyBundle\Util\Crypto $ crypto * /
$ crypto = $ this-> container-> get('mybundle.util.crypto' );
EncryptedString :: setCrypto($ crypto);
}
}

#app / Resources / config.yml
doctrine:
dbal:
types:
encrypted_string: MyBundle\Util\Crypto\Types\EncryptedString

#MyBundle / Resources / config / services.yml
services:
mybundle.util.crypto:
类:MyBundle\Util\Crypto
参数:[%key%]


I'm using Symfony 2 with Doctrine 2.

I need to encrypt a field in my entity using an encryption service, and I'm wondering where should I put this logic.

I'm using a Controller > Service > Repository architecture.

I was wondering if a listener would be a good idea, my main concern is, if my entity is stored encrypted, if I decrypt it on the fly its state it's gonna be changed and I'm not sure it's a good idea.

How would you implement this?

解决方案

To expand on richsage and targnation's great answers, one way to inject a dependency (e.g., cypto service) into a custom Doctrine mapping type, could be to use a static property and setter:

// MyBundle/Util/Crypto/Types/EncryptedString.php
class EncryptedString extends StringType
{
    /** @var \MyBundle\Util\Crypto */
    protected static $crypto;

    public static function setCrypto(Crypto $crypto)
    {
        static::$crypto = $crypto;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        $value = parent::convertToDatabaseValue($value, $platform);
        return static::$crypto->encrypt($value);
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        $value = parent::convertToPHPValue($value, $platform);
        return static::$crypto->decrypt($value);
    }

    public function getName()
    {
        return 'encrypted_string';
    }
}

Configuration would look like this:

// MyBundle/MyBundle.php
class MyBundle extends Bundle
{
    public function boot()
    {
        /** @var \MyBundle\Util\Crypto $crypto */
        $crypto = $this->container->get('mybundle.util.crypto');
        EncryptedString::setCrypto($crypto);
    }
}

# app/Resources/config.yml
doctrine:
    dbal:
        types:
            encrypted_string: MyBundle\Util\Crypto\Types\EncryptedString

# MyBundle/Resources/config/services.yml
services:
    mybundle.util.crypto:
        class: MyBundle\Util\Crypto
        arguments: [ %key% ]

这篇关于在哪里加密/解密我的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:52