canaryau/laravel-encryption 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

canaryau/laravel-encryption

最新稳定版本:3.0.3

Composer 安装命令:

composer require canaryau/laravel-encryption

包简介

A package to easily encrypt & decrypt model fields in Laravel using OpenSSL.

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

A package to easily encrypt & decrypt model fields in Laravel using OpenSSL.

Key Features

  • Encrypt and decrypt module attributes easily
  • Minimal configuration
  • Comes with a selection of eloquent builders for easy queries
  • Growing support for casting encryptable data

Installation

You can install the package via composer:

composer require canaryau/laravel-encryption

Usage

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Canaryau\LaravelEncryption\Traits\EncryptsAttributes;

class User extends Model
{
    use EncryptsAttributes;

    protected $encryptableAttributes = [
        'first_name',
        'last_name',
        'date_of_birth',
        'email_verified_at',
    ];

    protected $encryptableCasts = [
        'date_of_birth' => 'date',
        'email_verified_at' => 'datetime'
    ];
}

Configuration

Publshing The Configuration

The configuration file looks like this:

return [
    /**
     * Enable or disable the encryption.
     */
    'enabled' => env('LARAVEL_ENCRYPTION_ENABLED', true),

    /**
     * The encryption key.
     *
     * Default: your app key.
     */
    'key' => env('LARAVEL_ENCRYPTION_KEY', null),

    /**
     * The encryption cipher.
     *
     * Supports any cipher method supported by openssl_get_cipher_methods().
     *
     * Default: AES-256-CBC.
     */
    'cipher' => env('LARAVEL_ENCRYPTION_CIPHER', 'AES-256-CBC'),
];

If you need to make any changes to the configuration, feel free to publish the configuration file.

php artisan vendor:publish --provider="Canaryau\LaravelEncryption\LaravelEncryptionServiceProvider"

Configure Model Attributes To Be Encrypted

This package will only encrypt fields within the $encryptableAttributes array, leaving the rest of the model unencrypted and untouched.

protected $encryptableAttributes = [
    'first_name',
    'last_name',
];

This is useful for scenarios where compliance only requires you encrypting specific values and not your entire database.

Casting Encrypted Values

Due to the fact that encrypted values can be quite long, you will need to store them as text fields, meaning you have to give way to native column types.

Also due to the way Laravel handles casting (and the priority it takes) you cannot right now use native casts with encrypted fields.

So we use our own array to determine what fields should be casted to what:

protected $encryptableCasts = [
    'date_of_birth' => 'date',
];

Supported casts

Cast support is still growing, and will be added as time goes on.

  • date
  • datetime
  • json

Testing

composer test

License

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

统计信息

  • 总下载量: 271
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固