waseem/laravel-data-encryption

Automatically encryption and decryption data overriding by using getAttribute an setAttribute methods of Eloquent Model.

1.0.1 2023-06-25 18:23 UTC

This package is auto-updated.

Last update: 2025-04-25 22:56:12 UTC


README

A trait to encrypt data models in Laravel, this automatically encrypt and decrypt model data overriding getAttribute an setAttribute methods of Eloquent Model.

How to install

Run composer installation

    composer require waseem/laravel-data-encryption

Add ServiceProvider to your app/config.php file

    'providers' => [
        ...
        \Waseem\Encipher\EncipherServiceProvider::class,
    ],

Publish configuration file, this will create config/encrypt.php

     php artisan vendor:publish --provider=Waseem\Encipher\EncipherServiceProvider

How to use

  1. You must add SECRET_ENCRYPT_KEY and ENCRYPT_PREFIX in your .env file or set it in your config/encrypt.php file

  2. Use the Waseem\Encipher\Encipher trait:

    use Waseem\Encipher\Encipher;
  3. Set the $encipher array on your Model.

    protected $encipher = ['encrypted_property'];
  4. Here's a complete example:

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use Waseem\Encipher\Encipher;
    
    class User extends Model
    {
    
        use Encipher;
    
        /**
         * The attributes that should be encrypted when stored.
         *
         * @var array
         */
        protected $encipher = [ 'email', 'name' , 'mobile'];
     
        /**
        * Optionally you can define the attributes that should be converted to camelcase when retrieve.
        *
        * @var array
        */
         protected $camelcase = ['name'];
    }
  5. Optional. Encryption your current data

    if your lavarel application version 5.8+

    If you have current data in your database you can encrypt it with the: php artisan encipher:encryptionModel 'App\Models\User' command.

    Additionally you can decrypt it using the:php artisan encipher:decryptionModel 'App\Models\User' command.

    Note: You must implement first the Encipher trait and set $encipher attributes

  6. If you are using exists and unique rules with encrypted values replace it with exists_encrypted and unique_encrypted

    $validator = validator(['email'=>'wasi@demo.com'], ['email'=>'exists_encrypted:users,email']);

    OR

    $rules=array(
        'email' => 'required|unique_encrypted:users,email'
    );
    $messages=array(
        "email.unique_encrypted"=>"The email has already been taken."
    );
  7. You can still use where functions

    $validator = User::where('email','wasi@demo.com')->first();

    Automatically wasi@demo.com will be encrypted and pass it to the query builder.