使用PHP特性存储之前修改数据

我尝试使用PHP特性加密Laravel模型中的某些密钥

此特征已加密setattribute方法中的值,但数据未加密存储在数据库中,我不明白为什么会发生这种情况

当我在setattribute方法中使用dump时,一切正常,并且值已加密     

namespace App\EncryptorTraits;


use blackpanda\encryptor\Encryptor;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Encryption\Encrypter;

trait Encryptable
{

    public function setattribute($key,$val)
    {
        if($this->shouldEncrypt($key) && !$this->isEncrypted($val))
        {
            $val = $this->encryptAttribute($val);
        }

        return parent::setattribute($key,$val);
    }

    public function getattributeFromArray($key)
    {
        return $this->doDecryptAttribute($key,parent::getattributeFromArray($key));
    }

    public function getarrayableAttributes()
    {
        return $this->doDecryptAttributes(parent::getarrayableAttributes());
    }

    public function getattributes()
    {
        return $this->doDecryptAttributes(parent::getattributes());
    }





    public function doEncryptAttributes($key)
    {
        if($this->shouldEncrypt($key) && !$this->isEncrypted($this->attributes[$key])){
            $this->attributes[$key] = $this->encryptAttribute($this->attributes[$key]);
        }
    }

    public function encryptAttribute($value)
    {

        try {
            $encrypted = $this->getEncrypter()->encrypt($value);
        } catch (EncryptException $e) {
            throw new EncryptException($e->getMessage(),$e->getcode());
        }

        return $this->getEncryptionPrefix() . $encrypted;
    }


    public function decryptAttribute($value)
    {
        if( !$this->isEncrypted($value) ) return $value;

        try{
            $decrypted = $this->getEncrypter()->decrypt(str_replace($this->getEncryptionPrefix(),'',$value));
        }
        catch (DecryptException $e)
        {
            throw new DecryptException($e->getMessage(),$e->getcode());
        }

        return $decrypted;
    }

    public function doDecryptAttribute($key,$val)
    {
        if($this->shouldEncrypt($key) && $this->isEncrypted($val))
        {
            return $this->decryptAttribute($val);
        }

        return $val;
    }

    public function doDecryptAttributes($attributes)
    {
        foreach ($attributes as $key => $val)
        {
            $attributes[$key] = $this->doDecryptAttribute($key,$val);
        }

        return $attributes;
    }





    protected function getEncryptionPrefix()
    {
        return config('encryptor.db_encryption_prefix');
    }

    protected function getEncryptableList()
    {
        return (isset($this->encryptable)) ? $this->encryptable : [];
    }

    protected function shouldEncrypt($key) : bool
    {
        $encryptableList = $this->getEncryptableList();

        return (in_array($key,$encryptableList));
    }

    protected function isEncrypted($value)
    {
        return strpos((string)$value,$this->getEncryptionPrefix()) === 0;
    }

    protected function getEncrypter()
    {
        return new Encrypter($this->getEncryptionSecret(),'AES-256-CBC');
    }

    protected function getEncryptionSecret()
    {
        $encryptor = new Encryptor();
        return $encryptor->getDatabaseSecret();
    }
}

我尝试存储这样的数据

$Create = \App\myModel::create([
        'key1' => 'val1','key2' => 'val2',..
        ..
 ]);

我也尝试了Save方法,但是数据仍然未加密地存储在数据库中!

这是我的模特

<?php


namespace App;


use App\EncryptorTraits\Encryptable;
use Illuminate\Database\Eloquent\Model;

class myModel extends Model
{
    use Encryptable;

    protected $encryptable = ['name'];

    protected $guarded = ['id'];
    protected $table = 'table';

}
chenchongming 回答:使用PHP特性存储之前修改数据

我的一个项目具有相同的可加密特征。

<?php


namespace App\Traits\Models;

use Throwable;

trait Encryptable
{
    public function getEncryptable(): array
    {
        if (property_exists($this,'encryptable')) {
            return $this->encryptable;
        }

        return [];
    }

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

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

    public function getAttribute($key)
    {
        $value = parent::getAttribute($key);

        if ($value !== '' && in_array($key,true)) {
            try {
                $value = decrypt($value);
            } catch (Throwable $exception) {
                //
            }
        }

        return $value;
    }

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

        foreach ($this->getEncryptable() as $key) {
            if (isset($attributes[$key])) {
                $attributes[$key] = decrypt($attributes[$key]);
            }
        }

        return $attributes;
    }

    public function decrypt()
    {
        foreach ($this->getEncryptable() as $key) {
            parent::setAttribute($key,$this->{$key});
        }

        return $this;
    }
}
本文链接:https://www.f2er.com/3093548.html

大家都在问