rway7 / secure-eloquent
Encrypt your Eloquent model by user-provide key (with AES-128-CBC algorithm).
Installs: 1 546
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.6.0
- illuminate/database: ~5.4
- illuminate/encryption: ~5.4
- illuminate/support: ~5.4
Requires (Dev)
- phpunit/phpunit: ~5.0
README
Encrypt your Eloquent model by user-provide key (with AES-128-CBC algorithm).
Installation
To get started, install secure-eloquent
via Composer:
composer require rway7/secure-eloquent
Next, add the rway7\SecureEloquent\HasSecrets
trait to the model you want to encrypt.
This trait will give you the ability to encrypt specified attributes:
<?php namespace App; use rway7\SecureEloquent\HasSecrets; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasSecrets; /** * The attributes that need to be encrypted. * * @var array */ protected $secrets = [ 'title', 'body', ]; }
Finally, add a is_secured
column to your table:
Schema::create('posts', function (Blueprint $table) { // ... $table->boolean('is_secured')->default(false); });
Getting Started
Once a model has added that trait, you will be able to use secure
and unsecure
methods.
These methods will only affect the attributes that are specified in $secrets
property, and it will update the model's is_secured
attribute to indicates whether the model is encrypted.
Encrypt a model
$post->secure('encryption-key'); $post->title; // eyJpdiI6IndFTWFZTU... $post->body; // eyJpdiI6IkJ4ZThwNE... $post->save();
Note: It WILL NOT be encrypted if you
save
a model wihtout calling thesecure
method.
Decrypt a model
$post->unsecure('encryption-key'); $post->title; // Title $post->body; // Body
Determine if a model is encrypted
$post->secured(); // true