Code Monkey home page Code Monkey logo

laravel-encrypt-database's Introduction

Update

Currently, laravel has a model cast called "encrypted". Just use the cast, its easier and most efficient.

e.g:

//YourModel.php
protected $casts = [
'your_encrypted_field' => 'encrypted',
];

Laravel Encrypt Database

Automatic Encrypt and Decrypt your database data. Tested and used on Laravel 8.

Important

  • Note the key used to encrypt your data is your app_key, so keep it in a secure place.
  • If you lose it, you will lose all your database data.

  • The $casts attribute is not available for encrypted fields yet.

  • It is highly recommended to alter your encrypted column types to TEXT or LONGTEXT

Features

  • Minimal configuration
  • Encrypt and Decrypt database fields easily
  • Include searching encrypted data using the following: whereEncrypted and orWhereEncrypted
  • Include unique_encrypted, exists_encrypted rules
  • Uses openssl for encrypting and decrypting fields

Requirements

Laravel >= 8.0
PHP >= 7.4

Installing

Install the package

composer require wellingtoncarneirobarbosa/laravel-encrypt-database

Add the service provider on your providers list config/app.php:168

WellingtonCarneiroBarbosa\EncryptDatabase\Providers\EncryptDatabaseProvider::class,

Publish the config file

php artisan vendor:publish --tag=laravel-database-encryption

Usage

Just use the trait on yours encryptables models and list the encryptable fields.

use WellingtonCarneiroBarbosa\EncryptDatabase\Traits\EncryptableModel;

class User extends Authenticable
{
    Use HasFactory,
        EncryptableModel;
        
    /**
    * The attributes that should be encrypted.
    *
    * @var array
    */
    protected $encryptable = [
        'name',
        'email',
        'birth_date',
    ];
}

Note if you have a mutator on your model like "setNameAttribute" you should implement manually the encrypt method
public function setNameAttribute(string $value)
{
  $value = ucwords($value);
  
  $this->attributes['name'] = $this->encrypt('name', $value);
}

You also should implement manually decrypt method if you have an acessor method
public function getNameAttribute()
{
  $decrypted = $this->decrypt('name', $this->attributes['name']);

  $value = strtolower($decrypted);
  
  return $value;
}

If you are validating your form data with unique or exists you should replace it to unique_encrypted and exists_encrypted respectively

laravel-encrypt-database's People

Contributors

wellingtoncarneirobarbosa avatar

Stargazers

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