- Notifications
You must be signed in to change notification settings - Fork73
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
License
austinheap/laravel-database-encryption
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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 beinstalled without much effort to encrypt and decrypt Eloquent model attributes storedin your database tables, transparently. It is therefore highly opinionated but builtforconfiguration.
Whenenabled, it automagically begins encryptingdata as it is stored in the model attributes and decrypting data as it is recalled fromthe model attributes.
All data that is encrypted is prefixed with a header so that encrypted data can beeasily identified, encryption keys rotated, and (optionally) versioning of the encrypteddata format itself.
This supports columns that store either encrypted or non-encrypted data to make migrationeasier. Data can be read from columns correctly regardless of whether it is encrypted ornot but will be automatically encrypted when it is saved back into those columns. StandardLaravel Eloquent features like attribute casting will continue to work as normal, even ifthe underlying values stored in the database are encrypted by this package.
There isdocumentation forlaravel-database-encryption
online,the source of which is in thedocs/
directory. The most logical place to start are thedocs for theHasEncryptedAttributes
trait.
- Summary
- Requirements
- Status
- Schemas
- Installation
- Usage
- Keys and IVs
- Unit Tests
- Overrides
- FAQ
- Implementations
- Credits
- Contributing
- License
- Laravel: 5.5, 5.6, 5.7, or 5.8
- PHP: 7.1, 7.2, or 7.3
- PHPOpenSSL extension
Framework | Version | Release | Status | PHP v7.1 | PHP v7.2 | PHP v7.3 |
---|---|---|---|---|---|---|
Laravel | v5.5 | v0.1.0 (Packagist) | Stable | |||
Laravel | v5.6 | v0.1.1 (Packagist) | Stable | |||
Laravel | v5.7 | v0.2.0 (Packagist) | Stable | |||
Laravel | v5.8 | v0.2.1 (Packagist) | Stable |
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 tostore the encrypted values generated by this package.
If you are encrypting long strings such as JSON blobs then the encrypted values maybe longer than aVARCHAR
field can support, and you will need to alter your columntypes toTEXT
orLONGTEXT
.
The FAQ containsmigration instructions if you are moving from elocryptfive.
Via Composer command line:
$ composer require austinheap/laravel-database-encryption
Or add the package to yourcomposer.json
:
{"require": {"austinheap/laravel-database-encryption":"^0.2" }}
This package implements Laravel auto-discovery feature. After you install it the packageprovider and facade are added automatically.
If you would like to declare the provider and/or alias explicitly, you may do so by firstadding the service provider to yourconfig/app.php
file:
'providers' => [//AustinHeap\Database\Encryption\EncryptionServiceProvider::class,];
And then add the alias to yourconfig/app.php
file:
'aliases' => [//'DatabaseEncryption' =>AustinHeap\Database\EncryptionFacade::class,];
Publish the package config file:
$ php artisan vendor:publish --provider="AustinHeap\Database\Encryption\EncryptionServiceProvider"
You may now enable automagic encryption and decryption of Eloquent models by editing theconfig/database-encryption.php
file:
return ['enabled' =>env('DB_ENCRYPTION_ENABLED',true),];
Or simply setting the theDB_ENCRYPTION_ENABLED
environment variable to true, viathe Laravel.env
file or hosting environment.
DB_ENCRYPTION_ENABLED=true
Use theHasEncryptedAttributes
trait in any Eloquent model that you wish to apply encryptionto and define aprotected $encrypted
array containing a list of the attributes to encrypt.
For example:
useAustinHeap\Database\Encryption\Traits\HasEncryptedAttributes;class Userextends 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 beconverted to JSON and then encrypted.
For example:
useAustinHeap\Database\Encryption\Traits\HasEncryptedAttributes;class Userextends Eloquent {use HasEncryptedAttributes;protected$casts = ['extended_data' =>'array'];protected$encrypted = ['extended_data']; }
By including theHasEncryptedAttributes
trait, thesetAttribute()
andgetAttributeFromArray()
methods provided by Eloquent are overridden to include an additional step. This additional stepsimply 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.
The key and encryption algorithm used is the default LaravelEncrypter
service, and configured inyourconfig/app.php
:
'key' =>env('APP_KEY','SomeRandomString'),'cipher' =>'AES-256-CBC',
If you're usingAES-256-CBC
as the cipher for encrypting data, use the built in command to generateyour application key if you haven't already withphp artisan key:generate
. If you are encrypting longerdata, you may want to consider theAES-256-CBC-HMAC-SHA1
cipher.
The IV for encryption is randomly generated and cannot be set.
This package has aggressive unit tests built with the wonderfulorchestral/testbenchpackage which is built on top of PHPUnit. A MySQL server required for execution of unit tests.
There arecode coverage reports forlaravel-database-encryption
available online.
The following Laravel 5.5 methods from Eloquent are affected by this trait.
constructor()
-- callsfill()
.fill()
-- callssetAttribute()
which has been extended to encrypt the data.hydrate()
-- TBD.create()
-- callsconstructor()
and hencefill()
.firstOrCreate()
-- callsconstructor()
.firstOrNew()
-- callsconstructor()
.updateOrCreate()
-- callsfill()
.update()
-- callsfill()
.toArray()
-- callsattributesToArray()
.jsonSerialize()
-- callstoArray()
.toJson()
-- callstoArray()
.attributesToArray()
-- callsgetArrayableAttributes()
.getAttribute()
-- callsgetAttributeValue()
.getAttributeValue()
-- callsgetAttributeFromArray()
.getAttributeFromArray()
-- callsgetArrayableAttributes()
.getArrayableAttributes()
-- extended to decrypt data.setAttribute()
-- extended to encrypt data.getAttributes()
-- extended to decrypt data.castAttribute()
-- extended to cast encrypted data.isDirty()
-- extended to recognize encrypted data.
Yes! You can manually encrypt or decrypt data using theencryptedAttribute()
anddecryptedAttribute()
functions. For example:
$user =newUser();$encryptedEmail =$user->encryptedAttribute(Input::get('email'));
No! You will not be able to search on attributes which are encrypted by this package because...it is encrypted.Comparing encrypted values would require a fixed IV, which introduces security issues.
If you need to search on data then either:
- Leave it unencrypted, or
- Hash the data and search on the hash instead of the encrypted value using a well known hash algorithmsuch as
SHA256
.
You could store both a hashed and an encrypted value, using the hashed value for searching and retrievethe encrypted value as needed.
No! The same issue with searching also applies to authentication because authentication requires search.
Is this package compatible withelocryptfive
out-of-the-box?
No! While itis a (more modern) replacement, it is not compatible directly out of the box. To migrate to this package from elocryptfive, you must:
- Decrypt all the data in your database encrypted by elocryptfive.
- Remove any calls to elocryptfive from your models/code.
- Remove elocryptfive from your
composer.json
and runcomposer update
. - At this point you should have no encrypted data in your database and all calls/references, but make sure elocryptfive is completely purged.
- Follow the installation instructions above.
- ???
- Profit!
A pull request for automated migrations is more than welcome but is currently out of the scope of this project's goals.
Probably not! It's not feasible to guarantee interoperability between random packages out there, especially packages that also heavily modify Eloquent's default behavior.
Issues and pull requests regarding interoperability will not be accepted.
The following decently-trafficed sites use this package in production:
This is a fork ofdelatbabel/elocryptfive,which was a fork ofdtisgodsson/elocrypt,which was based on earlier work.
Pull requests welcome! Please seethe contributing guide for more information.
The MIT License (MIT). Please seeLicense File for more information.
About
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.