定制 lumensistemas/encryption-laravel 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lumensistemas/encryption-laravel

最新稳定版本:1.0.1

Composer 安装命令:

composer require lumensistemas/encryption-laravel

包简介

Sodium-based encryption, decryption, and blind indexing for Laravel applications.

README 文档

README

Latest Version on Packagist Tests Total Downloads

Sodium-based encryption, decryption, and blind indexing for Laravel. Provides Eloquent casts for transparent field-level encryption with optional authorization hooks (e.g. LGPD/GDPR compliance).

Installation

composer require lumensistemas/encryption-laravel

Publish the configuration file:

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

Generate key files:

php artisan encryption:generate-keys

This creates storage/encryption.key and storage/authentication.key with 0600 permissions.

You can specify a custom directory:

php artisan encryption:generate-keys --path=/etc/secrets

Use --force to overwrite existing key files.

Add the key file paths to .env:

ENCRYPT_ENC_KEY_PATH=/path/to/your/app/storage/encryption.key
ENCRYPT_AUTH_KEY_PATH=/path/to/your/app/storage/authentication.key

Security: Keys are read from files rather than environment variables. This prevents accidental exposure through phpinfo(), debug pages, logs, or process listings. Key files should have restrictive permissions (600) and be excluded from version control.

Usage

Facade

use LumenSistemas\Encrypt\Facades\Encryption;
use LumenSistemas\Encrypt\ValueObjects\SecretString;

// Encrypt & decrypt
$ciphertext = Encryption::encrypt(new SecretString('secret'));
$plaintext  = Encryption::decrypt($ciphertext)->get(); // 'secret'

// Hash & verify (blind index)
$hash = Encryption::hash(new SecretString('secret'));
Encryption::verify(new SecretString('secret'), $hash); // true

Eloquent Casts

AsEncryptedString

Encrypts on write, decrypts on read:

use LumenSistemas\Encrypt\Casts\AsEncryptedString;

class User extends Model
{
    protected $casts = [
        'cpf' => AsEncryptedString::class,
    ];
}

AsBlindIndex

Stores a deterministic hash for searching encrypted columns:

use LumenSistemas\Encrypt\Casts\AsBlindIndex;
use LumenSistemas\Encrypt\Casts\AsEncryptedString;

class User extends Model
{
    protected $casts = [
        'email_encrypted' => AsEncryptedString::class,
        'email_index'     => AsBlindIndex::class,
    ];
}

// Query by blind index
use LumenSistemas\Encrypt\Facades\Encryption;
use LumenSistemas\Encrypt\ValueObjects\SecretString;

$user = User::where('email_index', Encryption::hash(new SecretString($email)))->first();

Authorization & Audit Hook

Register a callback to authorize and/or log access before decryption. This is useful for LGPD/GDPR compliance:

use LumenSistemas\Encrypt\Casts\AsEncryptedString;

// In a ServiceProvider boot():
AsEncryptedString::authorizeUsing(function (Model $model, string $key) {
    // Log every access
    AuditLog::record(auth()->user(), $model, $key);

    // Deny or mask
    if (! auth()->user()->can('view-sensitive', $model)) {
        return '***.***.***-**'; // return masked value instead of decrypting
    }

    return true; // allow decryption
});

The callback receives (Model $model, string $key, array $attributes) and should:

  • Return true to allow decryption
  • Return a string to return a masked value (skips decryption entirely)
  • Throw an exception to deny access

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

统计信息

  • 总下载量: 68
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固