Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Encrypted Laravel Package

crudly/encrypted

View on GitHub
Deep Wiki
Context7

Encrypted

Build Status Release License

Note This package is no longer needed for new projects as hashing is now among the native casts and encryption was there for a while already. We will probably keep it up to date for a few more years because it usually only takes bumping a version tag.

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator