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

问题描述 投票:0回答:1

我尝试使用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';

}
php laravel eloquent traits
1个回答
0
投票
$ table-> password = bcrypt($ request-> input('password')); //密码加密

以这种方式,密码以加密形式存储

© www.soinside.com 2019 - 2024. All rights reserved.