PHPackages                             codelieutenant/laravel-crypto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. codelieutenant/laravel-crypto

ActiveLibrary[Security](/categories/security)

codelieutenant/laravel-crypto
=============================

Laravel Crypto is a package that provides a simple and easy to use API for encrypting, decrypting, hashing, and signing data using the latest PHP and Laravel features.

v2.0.0(2mo ago)181602[1 issues](https://github.com/MalusevDevelopment/laravel-crypto/issues)MITPHPPHP &gt;=8.4CI passing

Since Aug 5Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/MalusevDevelopment/laravel-crypto)[ Packagist](https://packagist.org/packages/codelieutenant/laravel-crypto)[ GitHub Sponsors](https://github.com/CodeLieutenant)[ RSS](/packages/codelieutenant-laravel-crypto/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (16)Used By (0)

Laravel Crypto
==============

[](#laravel-crypto)

[![Run Tests](https://github.com/dmalusev/laravel-crypto/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/dmalusev/laravel-crypto/actions/workflows/test.yml)[![GitHub issues](https://camo.githubusercontent.com/432fee6adad6575740b45d469155cbe58c7f8081724ef608b43b989c888e13ba/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6d616c757365763939382f4c61726176656c43727970746f3f6c6162656c3d476974687562253230497373756573)](https://github.com/malusev998/LaravelCrypto/issues)[![GitHub stars](https://camo.githubusercontent.com/60203f8c9e9a8dae60817315ba39bb5585768df8da10b0d51a843b5f920a5931/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6d616c757365763939382f4c61726176656c43727970746f3f6c6162656c3d4769746875622532305374617273)](https://github.com/malusev998/LaravelCrypto/stargazers)[![GitHub license](https://camo.githubusercontent.com/89e73efda3540bcbbb3f2971cf2a3adde73c02456adc43dbe55b8ffed9523811/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d616c757365763939382f4c61726176656c43727970746f3f6c6162656c3d4c6963656e6365)](https://github.com/malusev998/LaravelCrypto)

Laravel Crypto provides a simple and easy-to-use API for encrypting, decrypting, hashing, and signing data using modern cryptographic algorithms powered by `libsodium`.

Why Laravel Crypto?
-------------------

[](#why-laravel-crypto)

- **Modern Algorithms**: Support for XChaCha20-Poly1305, AES-256-GCM, AEGIS-128L, AEGIS-256, XSalsa20-Poly1305, Blake2b, and EdDSA.
- **Per-User Encryption**: Secure data using keys derived from and unique to each user, ensuring data privacy even if the `APP_KEY` is compromised.
- **Performance**: High-performance cryptographic operations utilizing hardware acceleration where available.
- **Drop-in Replacement**: Seamlessly replaces Laravel's default `EncryptionServiceProvider`.
- **Comprehensive**: Includes support for hashing, signing (symmetric and asymmetric), file encryption, Eloquent casting for encrypted files, and various data encoders (JSON, MessagePack, Igbinary).

Requirements
------------

[](#requirements)

- **PHP**: 8.4 or higher
- **Extensions**: `ext-sodium`
- **Laravel**: 10.x, 11.x, or 12.x

Getting Started
---------------

[](#getting-started)

### 1. Installation

[](#1-installation)

```
composer require codelieutenant/laravel-crypto
```

### 2. Service Provider Registration

[](#2-service-provider-registration)

In order to activate the package, you need to replace Laravel's default `EncryptionServiceProvider` with `CodeLieutenant\LaravelCrypto\ServiceProvider`.

#### Laravel 11.x &amp; 12.x

[](#laravel-11x--12x)

In `bootstrap/providers.php`, replace the default provider:

```
return [
    App\Providers\AppServiceProvider::class,
    // Illuminate\Encryption\EncryptionServiceProvider::class, // Remove or comment out
    CodeLieutenant\LaravelCrypto\ServiceProvider::class,       // Add this
];
```

#### Laravel 10.x

[](#laravel-10x)

In `config/app.php`, replace `Illuminate\Encryption\EncryptionServiceProvider::class` in the `providers` array:

```
'providers' => [
    // ...
    // Illuminate\Encryption\EncryptionServiceProvider::class,
    CodeLieutenant\LaravelCrypto\ServiceProvider::class,
    // ...
],
```

### 3. Configuration

[](#3-configuration)

Publish the configuration file:

```
php artisan vendor:publish --provider="CodeLieutenant\LaravelCrypto\ServiceProvider"
```

Update your `cipher` in `config/app.php`:

```
'cipher' => 'Sodium_AES256GCM', // Options: Sodium_AES256GCM, Sodium_XChaCha20Poly1305, Sodium_AEGIS256GCM, Sodium_AEGIS128LGCM, Sodium_SecretBox
```

### 4. Generating Keys

[](#4-generating-keys)

Generate the necessary cryptographic keys:

```
php artisan crypto:keys
```

This will update your `.env` file with the required keys and generate an EdDSA key pair in `storage/keys/`.

Usage Overview
--------------

[](#usage-overview)

### Encryption

[](#encryption)

Uses the standard Laravel `Crypt` facade but with Sodium algorithms.

```
use Illuminate\Support\Facades\Crypt;

$encrypted = Crypt::encryptString('Hello Sodium');
$decrypted = Crypt::decryptString($encrypted);
```

### File Encryption

[](#file-encryption)

Securely encrypt large files using chunked streaming.

```
use Illuminate\Support\Facades\Crypt;

// Encrypt a file
Crypt::encryptFile('path/to/input.txt', 'path/to/output.enc');

// Decrypt a file
Crypt::decryptFile('path/to/output.enc', 'path/to/decrypted.txt');
```

### Hashing

[](#hashing)

High-performance hashing using Blake2b.

```
use CodeLieutenant\LaravelCrypto\Facades\Hashing;

$hash = Hashing::hash('data');
```

### Signing

[](#signing)

Symmetric (HMAC) and Asymmetric (EdDSA) signing.

```
use CodeLieutenant\LaravelCrypto\Facades\Sign;

// HMAC
$sig = Sign::sign('message');

// EdDSA
$sig = Sign::eddsaSign('message');
```

### Eloquent Casting

[](#eloquent-casting)

Store files securely by automatically encrypting and decrypting them on-the-fly via an Eloquent caster.

```
use CodeLieutenant\LaravelCrypto\Casts\EncryptedFileCast;
use Illuminate\Database\Eloquent\Model;

class Document extends Model
{
    protected $casts = [
        'file' => EncryptedFileCast::class,
    ];
}

// Accessing the file decrypts it to a temporary location
$content = $document->file->contents();

// Modifying the content and saving the model re-encrypts the file
$document->file->putContents('Secret Data');
$document->save();
```

### User Encryption

[](#user-encryption)

Securely encrypt user data using their own unique encryption key.

```
use CodeLieutenant\LaravelCrypto\Casts\UserEncryptedWithIndex;
use Illuminate\Database\Eloquent\Model;

class UserSecret extends Model
{
    protected function casts(): array
    {
        return [
            'ssn' => UserEncryptedWithIndex::class . ':ssn_index',
        ];
    }
}
```

For more details on setting up and using per-user encryption, see the [User Encryption documentation](docs/UserEncryption.md).

Performance
-----------

[](#performance)

Benchmarks conducted on various data sizes (PHP 8.5.1, Sodium extension enabled) on a Macbook M4 Pro 48GB RAM.

### 1 KiB Payload

[](#1-kib-payload)

AlgorithmEncryptionDecryptionLaravel AES-256-CBC8.09 μs9.98 μsLaravel AES-256-GCM3.37 μs5.33 μs**Sodium AES-256-GCM**2.39 μs2.58 μs**Sodium AEGIS-128L****2.03 μs****2.14 μs****Sodium AEGIS-256**2.06 μs2.27 μsSodium XChaCha20-Poly13053.41 μs3.58 μs### 32 KiB Payload

[](#32-kib-payload)

AlgorithmEncryptionDecryptionLaravel AES-256-CBC151.01 μs181.81 μsLaravel AES-256-GCM35.81 μs81.98 μs**Sodium AES-256-GCM**26.93 μs29.22 μs**Sodium AEGIS-128L****18.23 μs****20.68 μs****Sodium AEGIS-256**18.86 μs21.82 μsSodium XChaCha20-Poly130558.40 μs60.90 μs### 1 MiB Payload

[](#1-mib-payload)

AlgorithmEncryptionDecryptionLaravel AES-256-CBC5.02 ms7.57 msLaravel AES-256-GCM1.31 ms3.94 ms**Sodium AES-256-GCM**1.11 ms1.88 ms**Sodium AEGIS-128L**0.90 ms**1.60 ms****Sodium AEGIS-256****0.82 ms**1.65 msSodium XChaCha20-Poly13052.21 ms2.90 msSodium-based algorithms provide more consistent performance and are significantly faster for decryption of large payloads compared to Laravel's default implementations. AEGIS algorithms offer top-tier performance on modern hardware.

Documentation
-------------

[](#documentation)

For detailed information, please refer to the following documentation:

- [Console Commands](docs/Commands.md)
- [Encryption](docs/Encryption.md)
- [User Encryption](docs/UserEncryption.md)
- [Hashing](docs/Hashing.md)
- [Signing](docs/Signing.md)
- [Utilities (Encoders, Base64, Random)](docs/Utilities.md)

License
-------

[](#license)

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

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance87

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 91.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~170 days

Recently: every ~181 days

Total

13

Last Release

60d ago

Major Versions

v0.1.0 → v1.0.0-alpha12024-02-27

v1.2.0 → v2.0.02026-02-26

v2.0.0 → v3.0.0-alpha12026-03-10

PHP version history (3 changes)v0.0.1PHP &gt;=7.3

v1.0.0-alpha1PHP &gt;=8.1

v1.2.0PHP &gt;=8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/33778979?v=4)[Dusan Malusev](/maintainers/CodeLieutenant)[@CodeLieutenant](https://github.com/CodeLieutenant)

---

Top Contributors

[![CodeLieutenant](https://avatars.githubusercontent.com/u/33778979?v=4)](https://github.com/CodeLieutenant "CodeLieutenant (91 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")

---

Tags

algorithmbase64base64urlblake2bblake2b-hash-algorithmcryptographyhashinghashing-algorithmslaravelprogrammersrandom-generationsha256laravelsecuritycryptographyencryptioncryptosigninghashingsodiumdecryption

###  Code Quality

TestsPest

Static AnalysisRector

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/codelieutenant-laravel-crypto/health.svg)

```
[![Health](https://phpackages.com/badges/codelieutenant-laravel-crypto/health.svg)](https://phpackages.com/packages/codelieutenant-laravel-crypto)
```

###  Alternatives

[phpseclib/phpseclib

PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.

5.6k434.8M1.3k](/packages/phpseclib-phpseclib)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[ercsctt/laravel-file-encryption

Secure file encryption and decryption for Laravel applications

642.6k](/packages/ercsctt-laravel-file-encryption)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
