Download the PHP package thomascombe/encryptable-fields without Composer
On this page you can find all versions of the php package thomascombe/encryptable-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thomascombe/encryptable-fields
More information about thomascombe/encryptable-fields
Files in thomascombe/encryptable-fields
Package encryptable-fields
Short Description Laravel Backpack Fields Encryptable
License MIT
Homepage https://github.com/webqamdev/encryptable-fields
Informations about the package encryptable-fields
Eloquent model encrypted fields for Laravel
Allow you to encrypt model's fields. You can add a hashed field to allow SQL query.
Installation
You can install the package via composer:
You can publish the configuration via Artisan:
Usage
To work with this package, you need to use our EncryptableFields
trait in your models, then override
the $encryptable
property. This array allows you to define encryptable attributes in your model.
You can also add attributes to contain a hash of the non encrypted value, which might be useful in order to execute a fast full match for a given searched value.
To do so, you need to use the $encryptable
property as an associative array, where encryptable attributes are keys and
associated hashed attributes are values.
To create a new model, simply do it as before:
To find a model from a hashed value:
or use the model's local scope:
Authentication
An auth provider, eloquent-hashed
, is registered by this package and allows to authenticate users on a hashed
attribute, per example an email. To use it, simply change your auth configuration as follows:
Searchable encrypted values
MySQL
and MariaDB both provide an aes_decrypt
function, allowing to decrypt values
directly when querying. It then becomes possible to use this function to filter or sort encrypted values.
However, Laravel default encrypter only handles AES-128-CBC
and AES-256-CBC
cipher methods, where MySQL
and MariaDB
requires AES-128-ECB
. We're going to use two different keys.
To do so, add the following variable to your .env
file:
and run php artisan encryptable-fields:key-generate
command to generate a database encryption key.
⚠️ You shouldn't generate this key on your own because ciphers differ between Laravel and MySQL/MariaDB.
Then, it is required to override Laravel's default encrypter, which is done in DatabaseEncrypter.php.
Include DatabaseEncryptionServiceProvider in
your config/app.php
, so that a singleton instance will be registered in your project, under databaseEncrypter
key:
Finally, override the package configuration in encryptable-fields.php
file:
⚠️ With DatabaseEncrypter.php, values are not serialized in order to allow
querying with exact values (=
instead of like
operator), which means it won't handle object instances or arrays.
If you're using Laravel Backpack in your project, a trait EncryptedSearchTrait provides methods to customize search and order logics.
Validation
This package comes with some rules to validate existence and uniqueness for a hashed or encrypted attribute.
They work as extensions for Illuminate\Validation\Rules\Exists
and Illuminate\Validation\Rules\Unique
.
Hashed
and
Encrypted
and
Hide decrypt value in log
If your application use spatie/laravel-activitylog
or webqamdev/activity-logger-for-laravel :
Add HasEncryptableFieldsLog
trait in each model with logs.
This trait print encrypted values in log instead of decrypt values.
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.