garethnic/laracrypt

Add database encryption to laravel

dev-master / 1.0.x-dev 2016-03-16 10:24 UTC

This package is not auto-updated.

Last update: 2024-11-09 20:06:56 UTC


README

Latest Version on Packagist Software License Total Downloads

A Laravel package to handle database encryption. In the current state of this project, only symmetric encryption is possible.

Install

Make sure you have libsodium installed.

Via Composer

Add the following to your composer.json file:

"garethnic/laracrypt": "dev-master"

Add the garethnic\ServiceProvider to your config/app.php providers array:

garethnic\laracrypt\LaraCryptServiceProvider::class,

Then do:

$ php artisan vendor:publish

To copy the config file over. In this file you can specify where to store and load your key from.

'path' => storage_path('keys/encryption.key')

For your database schema make sure the encrypted columns are of type BLOB.

Usage

You will first need to create your key:

$ 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.

In the future more options will be added to make key storage more flexible.

There are multiple ways to go about encrypting/decrypting:

AppServiceProvider

// Encrypting all attributes
Post::saving(function ($post) {
    foreach($post['attributes'] as $key => $value) {
        $post->$key = LaraCrypt::encrypt($value);
    }
});

// Encrypting all attributes
Post::saving(function ($post) {
    $post->body = LaraCrypt::encrypt($post->body);
});

Or in your Model you could do:

public function setBodyAttribute($value)
{
    $this->attributes['title'] = LaraCrypt::encrypt($value);
}

Decrypting:

It's as simple as LaraCrypt::decrypt($text).

In your Model:

public function getBodyAttribute($value)
{
    return LaraCrypt::decrypt($value);
}

TODO

  • Add configuration options
  • Add support for asymmetric encryption
  • Clean up code
  • Write tests

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

License

The MIT License (MIT). Please see License File for more information.