Laravel Eloquent - 随时加密/解密数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24687597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:47:59  来源:igfitidea点击:

Laravel Eloquent - Encrypting/Decrypt Data on call

encryptionlaravellaravel-4eloquent

提问by Kousha

I can use Cryptto encrypt/decrypt my data. I want to encrypt some information in my db (such as the name, email, phone number to name a few).

我可以Crypt用来加密/解密我的数据。我想加密我的数据库中的一些信息(例如姓名、电子邮件、电话号码等等)。

Assuming that I want EVERYTHING to be encrypted, I want to be able to do this in the background by itself, which I can perform by overwriting the createand savefunctions:

假设我希望对所有内容进行加密,我希望能够在后台自行执行此操作,我可以通过覆盖createsave函数来执行此操作:

// For instance, the save() function could become
public function save(array $options = array())
{
    foreach ($this->attributes as $key => $value)
    {
        if (isset($value)) $this->attributes[$key] = Crypt::encrypt($value);
    }
    return parent::save($options);
}

Now, I want the decryption to be performed the same way, so that when I say User::find($id), the returned $useris already decrypted. Also other functions such as firstOrFail()get()first()and all to work as well.

现在,我希望以相同的方式执行解密,以便当我说 时User::find($id),返回$user的已经解密。还有其他功能,如firstOrFail()get()first()和 都可以正常工作。

I also would like this functionality to be extended when I use relationships (so User::with('someOtherTable')->find($id)also work).

我还希望在使用关系时扩展此功能(因此User::with('someOtherTable')->find($id)也可以工作)。

Would this be possible? If this is not possible, I am thinking of creating a helper function decyrpt()

这可能吗?如果这是不可能的,我正在考虑创建一个辅助函数decyrpt()

function decrypt($array)
{
    if (!is_array($array)) return Crypt::decrypt($array);

    $result = [];

    foreach($array as $key => $value) $result[$key] = decrypt($value);

    return $result;
}

And pass all my results through this first, and then start using them, but it would be nicer if Laravel would provide this, or if there was a "Laravel Way" of doing this.

并首先通过我的所有结果,然后开始使用它们,但是如果 Laravel 提供这个,或者如果有一个“Laravel 方式”来做到这一点会更好。

回答by Joseph Silber

It doesn't really make sense to encrypt everything. For example, you never want to encrypt the primary key; that doesn't even make sense. Likewise you probably don't want to encrypt the date fields; you'll lose the ability to perform any sort of SQL query on them.

加密一切真的没有意义。例如,您永远不想加密主键;这甚至没有意义。同样,您可能不想加密日期字段;您将无法对它们执行任何类型的 SQL 查询。

With that in mind, you can try something like this:

考虑到这一点,您可以尝试以下操作:

class BaseModel extends Eloquent {

    protected $encrypt = [];

    public function setAttribute($key, $value)
    {
        if (in_array($key, $this->encrypt))
        {
            $value = Crypt::encrypt($value);
        }

        return parent::setAttribute($key, $value);
    }

    public function getAttribute($key)
    {
        if (in_array($key, $this->encrypt))
        {
            return Crypt::decrypt($this->attributes[$key]);
        }

        return parent::getAttribute($key);
    }

    public function attributesToArray()
    {
        $attributes = parent::attributesToArray();

        foreach ($attributes as $key => $value)
        {
            if (in_array($key, $this->encrypt))
            {
                $attributes[$key] = Crypt::decrypt($value);
            }
        }

        return $attributes;
    }

}

then have all you models extend this one, and set the $encryptproperty to whatever columns you want encrypted for that particular model.

然后让你所有的模型扩展这个,并将$encrypt属性设置为你想要为该特定模型加密的任何列。



P.S.If you want to use Eloquent's accessor functionality, you'll have to play with this a bit more.

PS如果你想使用 Eloquent 的访问器功能,你必须多玩一点。

回答by Logus

It's worth mentioning Elocryptlibrary for Laravel 4. It's a more elaborate solution that works the same way. If you're using Laravel 5 use this one instead: Elocrypt 5.

值得一提的是 Laravel 4 的Elocrypt库。这是一个更精细的解决方案,工作方式相同。如果您使用的是 Laravel 5,请改用这个:Elocrypt 5