# Laravel 5.5+ Database Encryption Package

[](https://github.com/austinheap/laravel-database-encryption/blob/master/LICENSE.md)
[](https://github.com/austinheap/laravel-database-encryption/releases)
[](https://packagist.org/packages/austinheap/laravel-database-encryption)
[](https://travis-ci.org/austinheap/laravel-database-encryption)
[](https://gemnasium.com/github.com/austinheap/laravel-database-encryption)
[](https://scrutinizer-ci.com/g/austinheap/laravel-database-encryption/)
[](https://styleci.io/repos/113929569)
[](https://codeclimate.com/github/austinheap/laravel-database-encryption/maintainability)
[](https://codeclimate.com/github/austinheap/laravel-database-encryption/test_coverage)
## A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
The purpose of this project is to create a set-it-and-forget-it package that can be
installed without much effort to encrypt and decrypt Eloquent model attributes stored
in your database tables, transparently. It is therefore highly opinionated but built
for configuration.
When enabled, it automagically begins encrypting data as it is stored in the model
attributes and decrypting data as it is recalled from the model attributes.
All data that is encrypted is prefixed with a header so that encrypted data can be
easily identified, encryption keys rotated, and (optionally) versioning of the encrypted
data format itself.
This supports columns that store either encrypted or non-encrypted data to make migration
easier. Data can be read from columns correctly regardless of whether it is encrypted or
not but will be automatically encrypted when it is saved back into those columns. Standard
Laravel Eloquent features like attribute casting will continue to work as normal, even if
the underlying values stored in the database are encrypted by this package.
There is [documentation for `laravel-database-encryption` online](https://austinheap.github.io/laravel-database-encryption/),
the source of which is in the [`docs/`](https://github.com/austinheap/laravel-database-encryption/tree/master/docs)
directory. The most logical place to start are the [docs for the `HasEncryptedAttributes` trait](https://austinheap.github.io/laravel-database-encryption/classes/AustinHeap.Database.Encryption.Traits.HasEncryptedAttributes.html).
## Table of Contents
* [Summary](#a-package-for-automatically-encrypting-and-decrypting-eloquent-attributes-in-laravel-55-based-on-configuration-settings)
* [Requirements](#requirements)
* [Schemas](#schemas)
* [Installation](#installation)
+ [Step 1: Composer](#step-1-composer)
+ [Step 2: Enable the package (Optional)](#step-2-enable-the-package-optional)
+ [Step 3: Configure the package](#step-3-configure-the-package)
* [Usage](#usage)
* [Keys and IVs](#keys-and-ivs)
* [Unit Tests](#unit-tests)
* [Overrides](#overrides)
* [FAQ](#faq)
+ [Can I manually encrypt or decrypt arbitrary data?](#can-i-manually-encrypt-or-decrypt-arbitrary-data)
+ [Can I search encrypted data?](#can-i-search-encrypted-data)
+ [Can I encrypt all my `User` model data?](#can-i-encrypt-all-my-user-model-data)
+ [Is this package compatible with elocryptfive out-of-the-box?](#is-this-package-compatible-with-elocryptfive-out-of-the-box)
* [Implementations](#implementations)
* [Credits](#credits)
* [Contributing](#contributing)
* [License](#license)
## Requirements
* Laravel 5.5.*
* PHP >= 7.1.0
* PHP [OpenSSL extension](http://php.net/manual/en/book.openssl.php)
## Schemas
Encrypted values are usually longer than plain text values, sometimes much longer.
You may find that the column widths in your database tables need to be altered to
store the encrypted values generated by this package.
If you are encrypting long strings such as JSON blobs then the encrypted values may
be longer than a `VARCHAR` field can support, and you will need to alter your column
types to `TEXT` or `LONGTEXT`.
The FAQ contains [migration instructions if you are moving from elocryptfive](#is-this-package-compatible-with-elocryptfive-out-of-the-box).
## Installation
### Step 1: Composer
Via Composer command line:
```bash
$ composer require austinheap/laravel-database-encryption
```
Or add the package to your `composer.json`:
```json
{
"require": {
"austinheap/laravel-database-encryption": "0.1.0"
}
}
```
### Step 2: Enable the package (Optional)
This package implements Laravel 5.5's auto-discovery feature. After you install it the
package provider and facade are added automatically.
If you would like to declare the provider and/or alias explicitly, you may do so by first
adding the service provider to your `config/app.php` file:
```php
'providers' => [
//
AustinHeap\Database\Encryption\EncryptionServiceProvider::class,
];
```
And then add the alias to your `config/app.php` file:
```php
'aliases' => [
//
'DatabaseEncryption' => AustinHeap\Database\EncryptionFacade::class,
];
```
### Step 3: Configure the package
Publish the package config file:
```bash
$ php artisan vendor:publish --provider="AustinHeap\Database\Encryption\EncryptionServiceProvider"
```
You may now enable automagic encryption and decryption of Eloquent models by editing the
`config/database-encryption.php` file:
```php
return [
'enabled' => env('DB_ENCRYPTION_ENABLED', true),
];
```
Or simply setting the the `DB_ENCRYPTION_ENABLED` environment variable to true, via
the Laravel `.env` file or hosting environment.
```bash
DB_ENCRYPTION_ENABLED=true
```
## Usage
Use the `HasEncryptedAttributes` trait in any Eloquent model that you wish to apply encryption
to and define a `protected $encrypted` array containing a list of the attributes to encrypt.
For example:
```php
use AustinHeap\Database\Encryption\Traits\HasEncryptedAttributes;
class User extends Eloquent {
use HasEncryptedAttributes;
/**
* The attributes that should be encrypted on save.
*
* @var array
*/
protected $encrypted = [
'address_line_1', 'first_name', 'last_name', 'postcode'
];
}
```
You can combine `$casts` and `$encrypted` to store encrypted arrays. An array will first be
converted to JSON and then encrypted.
For example:
```php
use AustinHeap\Database\Encryption\Traits\HasEncryptedAttributes;
class User extends Eloquent {
use HasEncryptedAttributes;
protected $casts = ['extended_data' => 'array'];
protected $encrypted = ['extended_data'];
}
```
By including the `HasEncryptedAttributes` trait, the `setAttribute()` and `getAttributeFromArray()`
methods provided by Eloquent are overridden to include an additional step. This additional step
simply checks whether the attribute being accessed via setter/getter is included in the `$encrypted`
array on the model, and then encrypts or decrypts it accordingly.
## Keys and IVs
The key and encryption algorithm used is the default Larave