LaraCrypt
![Software License][ico-license]
, (*1)
A Laravel package to handle database encryption. In the current state of this project, only symmetric encryption is possible., (*2)
Install
Make sure you have libsodium installed., (*3)
Via Composer, (*4)
Add the following to your composer.json
file:, (*5)
"garethnic/laracrypt": "dev-master"
Add the garethnic\ServiceProvider to your config/app.php providers array:, (*6)
``` php
garethnic\laracrypt\LaraCryptServiceProvider::class,, (*7)
Then do:
``` bash
$ php artisan vendor:publish
To copy the config file over. In this file you can specify where to store and load your key from., (*8)
``` php
'path' => storage_path('keys/encryption.key'), (*9)
**For your database schema make sure the encrypted columns are of type `BLOB`.**
## Usage
You will first need to create your key:
``` bash
$ php artisan laracrypt:key
This will generate a key and save it in storage/keys
. You could also generate the key programmatically via the static generateKey
method., (*10)
In the future more options will be added to make key storage more flexible., (*11)
There are multiple ways to go about encrypting/decrypting:, (*12)
AppServiceProvider
, (*13)
``` php
// Encrypting all attributes
Post::saving(function ($post) {
foreach($post['attributes'] as $key => $value) {
$post->$key = LaraCrypt::encrypt($value);
}
});, (*14)
// Encrypting all attributes
Post::saving(function ($post) {
$post->body = LaraCrypt::encrypt($post->body);
});, (*15)
Or in your `Model` you could do:
``` php
public function setBodyAttribute($value)
{
$this->attributes['title'] = LaraCrypt::encrypt($value);
}
Decrypting:, (*16)
It's as simple as LaraCrypt::decrypt($text)
., (*17)
In your Model
:, (*18)
``` php
public function getBodyAttribute($value)
{
return LaraCrypt::decrypt($value);
}, (*19)
## TODO
* Add configuration options
* Add support for asymmetric encryption
* Clean up code
* Write tests
## Change log
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## Testing
``` bash
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details., (*20)
License
The MIT License (MIT). Please see License File for more information., (*21)