Encryption cast for Eloquent

Overview

Encrypted

Build Status Release License

A custom cast class for Laravel Eloquent that encrypts or hashes your values. Package is small and provides just a few, simple, well tested features.

protected $casts = [
    // hashes the value when assigning to $model->password
    'password' => Password::class,

    // encrypts on write, decrypts on read
    'classified' => Encrypted::class,

    // encrypts on write, decrypts & typecasts on read
    'secret' => Encrypted::class.':integer',
];

Installation

Use composer.

$ composer require crudly/encrypted

Usage

Mark any column in your model as encrypted.

<?php

namespace App;

use Crudly\Encrypted\Encrypted;
use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
	protected $casts = [
		'something_secret' => Encrypted::class,
	];
}

You can work with the attribute as you normally would, but it will be encrypted on the database.

$mm = new MyModel;

$mm->someting_secret = 'classified_info';
$mm->save();

Type casting

Encryption serializes the variable and decryption unserializes it, so you get out exactly what you put in. This usually means that no type casting is needed.

But sometimes you want everything casted to some type even if you put something else in. In those cases you can specify types (all of Eloquent's default casts are supported):

<?php

namespace App;

use Crudly\Encrypted\Encrypted;
use Illuminate\Database\Eloquent\Model;

class MyModel extends Model
{
	protected $casts = [
		'encrypted_column' => Encrypted::class,
		'an_integer' => Encrypted::class.':integer',
		'a_string' => Encrypted::class.':string',
		'decimal_with_two_places' => Encrypted::class.':decimal:2',
	];
}

Password hashing

This can also be used to hash a password upon write.

<?php

namespace App;

use Crudly\Encrypted\Password;
use Illuminate\Database\Eloquent\Model;

class MyUser extends Model
{
	protected $casts = [
		'password' => Password::class,
	];
}

This hashes the password using bcrypt. You can check a string against the hashed password using Hash facade.

$mu = new MyUser;
$mu->password = 'secret';

$mu->password; // returns a hash

Hash::check('secret', $mu->password); //returns true
Hash::check('hunter2', $mu->password); //returns false

TODO

Maybe add key and cipher customization via options, i.e. Encrypted::class.':string,AckfSECXIvnK5r28GVIWUAxmbBSjTsmF' and Encrypted::class.':string,AckfSECXIvnK5r28GVIWUAxmbBSjTsmF,AES-128-CBC'. And password hashing options.

You might also like...
Laravel Ban simplify blocking and banning Eloquent models.
Laravel Ban simplify blocking and banning Eloquent models.

Laravel Ban Introduction Laravel Ban simplify management of Eloquent model's ban. Make any model bannable in a minutes! Use case is not limited to Use

Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react? Create presenters for Eloquent Models
Create presenters for Eloquent Models

Laravel Presentable This package allows the information to be presented in a different way by means of methods that can be defined in the model's pres

Orbit is a flat-file driver for Laravel Eloquent
Orbit is a flat-file driver for Laravel Eloquent

Orbit is a flat-file driver for Laravel Eloquent. It allows you to replace your generic database with real files that you can manipulate using the methods you're familiar with.

Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

A small package for adding UUIDs to Eloquent models.

A small package for adding UUIDs to Eloquent models. Installation You can install the package via composer: composer require ryangjchandler/laravel-uu

The missing laravel helper that allows you to inspect your eloquent queries with it's bind parameters

Laravel Query Inspector The missing laravel helper that allows you to ispect your eloquent queries with it's bind parameters Motivations Let's say you

Laravel Quran is static Eloquent model for Quran.

Laravel Quran بِسْمِ ٱللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ Laravel Quran is static Eloquent model for Quran. The Quran has never changed and never will, bec

Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Laravel Eloquent UUID slug Summary About Features Requirements Installation Examples Compatibility table Alternatives Tests About By default, when get

Comments
  • Using datetime cast on Encrypted fails

    Using datetime cast on Encrypted fails

    I'm using Laravel 7.9, and using the Encrypted cast using a datetime cast, it fails:

    class Student extends Model
    {
        protected $casts = [
            'birth_date' => Encrypted::class . ':datetime',
        ];
    }
    
    $student->birth_date; // fails
    

    Call to undefined method "getConnection".

    It looks like HasAttributes is calling the getConnection() method, which is in Model to get the date format if a date format is not set.

    I think the simplest solution would be setting the $dateFormat field in Caster, although it's really only needed for date fields.

    opened by rawilk 1
Releases(1.4.0)
Owner
null
Make your own custom cast type for Laravel model attributes

Laravel Custom Casts Make your own cast type for Laravel model attributes Laravel custom casts works similarly to Eloquent attribute casting, but with

Vladimir Ković 220 Oct 28, 2022
Ani Cast - Anime List & Trending App. (Powered by Jikan API)

(Under Development) Ani Cast - Anime Shows App.

Noval 2 Jun 15, 2022
Strongly typed settings for Laravel, includes built-in encryption and friendly validation.

Strongly Typed Laravel Settings Install composer require bogdankharchenko/typed-laravel-settings Model Setup namespace App\Models\User; use Illuminat

Bogdan Kharchenko 10 Aug 31, 2022
Simple Video is a automated H264 encryption system built on Lumen Laravel Framework

Simple Video is a automated H264 encryption system built on Lumen Laravel Framework

Azril Nazli Alias 4 Oct 5, 2022
An Eloquent Way To Filter Laravel Models And Their Relationships

Eloquent Filter An Eloquent way to filter Eloquent Models and their relationships Introduction Lets say we want to return a list of users filtered by

Eric Tucker 1.5k Jan 7, 2023
Easy creation of slugs for your Eloquent models in Laravel

Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you

Colin Viebrock 3.6k Dec 30, 2022
Sortable behaviour for Eloquent models

Sortable behaviour for Eloquent models This package provides a trait that adds sortable behaviour to an Eloquent model. The value of the order column

Spatie 1.2k Dec 22, 2022
Turn any Eloquent model into a list!

Listify Turn any Eloquent model into a list! Description Listify provides the capabilities for sorting and reordering a number of objects in a list. T

Travis Vignon 138 Nov 28, 2022
This package gives Eloquent models the ability to manage their friendships.

Laravel 5 Friendships This package gives Eloquent models the ability to manage their friendships. You can easily design a Facebook like Friend System.

Alex Kyriakidis 690 Nov 27, 2022
Automatically validating Eloquent models for Laravel

Validating, a validation trait for Laravel Validating is a trait for Laravel Eloquent models which ensures that models meet their validation criteria

Dwight Watson 955 Dec 25, 2022