Git Product home page Git Product logo

encryption-db-fields's Introduction

Encryption DB Fields

This package allows two way encryption on all critical fields inside the database.

Allows search by encrypted field.

Installation

You can install the package via composer:

composer require oriamdev/encryption-db-fields

Setup Eloquent Model

1 - First, you need to add the trait EncryptAttributesTrait to the model when you want to encrypt fields.

    use EncryptAttributesTrait;

2 - Define the array with fields to be encrypted

    protected array $encrypts = ['name', 'email'];

3 - If you want to search by encrypted field you need to add the array $searchableEncrypts to the model.

    protected array $searchableEncrypts = ['name', 'email'];

Adapt DB Table structure

NOTES:

  • The fields to be encrypted need to be text type.
  • For each searchable field you need to add a string field to migration with name field_signature
Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->text('name'); 
    $table->text('email');
    $table->string('email_signature',10)->index();
    $table->string('name_signature', 10)->index();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Migrate database

php artisan migrate

CRUD Operations

The crud operations work the same way and the process to encrypt and decrypt fields are responsibility of the trait EncryptAttributesTrait

Validation Rules

The laravel validation rules unique and exists does not work but, not worry about it. The package has a Validation Rule Oriamdev\EncryptionUserDbAuth\Rules\Encrypted The construct need the fully qualified name of the model. User::class or Post::class

Check if field under validation is unique

After creating the new instance of Encrypted Rule you can pass the unique method

$request->validate([
     'email' => [(new Encrypted(User::class))->unique()],
]);

If the validation fails the rule return the default value of validation:unique

Check if field under validation exists

After creating the new instance of Encrypted Rule you can pass the exists method

$request->validate([
     'email' => [(new Encrypted(User::class))->unique()],
]);

If the validation fails the rule return the default value of validation:unique

$request->validate([
    'name' => [(new Encrypted(User::class))->exists()]
]);

Search by encrypted field

$user = User::findByEncrypt('email', '[email protected]');

The method findByEncrypt return an instance of model or null if not exists.

encryption-db-fields's People

Contributors

oriamdev avatar

Stargazers

 avatar

Watchers

 avatar  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.