Code Monkey home page Code Monkey logo

laravel-model-encryptor's Introduction

Laravel Model Encryptor

Software License PHP Version Latest Version on Packagist

A model trait for flexible encryption and decryption of data

Installation

You can install the package via composer:

composer require pmingram/laravel-model-encryptor

Usage and Configuration

This package is a trait that can be added to any Laravel model you wish to apply encryption to:

use PmIngram\Laravel\ModelEncryptor\HasEncryption;

class ModelName extends Model
{
    use HasEncryption;
}

By default, the trait will apply encryption to any record on creation - but to actually encrypt data, you need to configure some properties within your model:

Property Type Description Default Value
$encryptOnCreate Boolean Enable or disable encryption on model creation. If set to false, the model can be encrypted later by invoking $model->encrypt(true). true
$encryptionKey String A random string to act as a base encryption key for the model in conjunction with the application key set by Laravel. If left blank, the Laravel application key will be used alone. Empty string
$encryptionSaltColumn String The data in the column defined here will be appended to the $encryptionKey to create a per-row encryption key. This should not be a column in the $encryptionColumnKeys array, as this could lead to data loss. Empty string
$encryptionColumnKeys Array List of columns to be included in the encryption and decryption processes. These columns should be defined as LONGTEXT datatypes (or the equivalent type in your database engine.) Empty array

There is no specific scope requirement for these properties, but it is recommended to use a protected scope.

Encryption and Decryption

Models can be encrypted and decrypted easily with a simple method call:

$model->encrypt();
$model->decrypt();

This will encrypt or decrypt the data within the current model instance, but will not persist the change to the database. This is particularly useful in the event you wish to decrypt the data for presentation (for example, in a view or in a resource for an API endpoint) but you want to keep that data encrypted in the database.

To persist to the database, simply pass true as an optional argument within the method:

$model->encrypt(true);
$model->decrypt(true);

Example Configuration

Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use PmIngram\Laravel\ModelEncryptor\HasEncryption;

class ExampleModel extends Model
{
    use HasEncryption, HasFactory;

    protected $encryptOnCreate = true;
    protected $encryptionKey = '';
    protected $encryptionSaltColumn = 'column_b';
    protected $encryptionColumnKeys = [
        'column_c', 'column_e',
    ];

    protected $fillable = [
        'column_a', 'column_b', 'column_c', 'column_d', 'column_e',
    ];

    public function __construct(array $attributes = [])
    {
        $this->encryptionKey = config('encryptionkeys.model.example');

        parent::__construct($attributes);
    }
}

Laravel Configuration File - "encryptionkeys.php"

<?php

return [
    'models' => [
        'example' => env('ENCKEY_MODEL_EXAMPLE', null),
    ]
];

Environment Variable (.env)

ENCKEY_MODEL_EXAMPLE=somerandomstring

Recommendation on Encryption Keys and Security

While it is entirely possible to store the model-level encryption key within the model itself, as a string in the $encryptionKey property, it is strongly advised to abstract the string out to a configuration file as per the example above, then use the .env file to set the strings.

It is both bad practice and a security risk to store encryption keys and passwords within a codebase, especially when that codebase is persisted to a VCS such as Git or SVN.

Important Note

The Laravel application key is used with this trait. If the application key is changed, any encrypted data will no longer be readable. Of course this is the case with any encryption routines deployed, but should be considered if you need to change your application's key.

License

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

laravel-model-encryptor's People

Contributors

paulmichaeldev avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.