定制 anexia/laravel-encryption 二次开发

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

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

anexia/laravel-encryption

Composer 安装命令:

composer require anexia/laravel-encryption

包简介

Encryption and decryption for eloquent models

README 文档

README

A Laravel package that adds database encryption support to eloquent models.

1. Installation and configuration

1. Install via composer

Install the module via composer, therefore adapt the require part of your composer.json:

"require": {
    "anexia/laravel-encryption": "1.0.0"
}

Now run

composer update [-o]

to add the packages source code to your /vendor directory and update the autoloading.

2. Add service provider to app config

'providers' => [
    /*
     * Package Service Providers...
     */
    \Anexia\LaravelEncryption\DatabaseEncryptionServiceProvider::class,
]

3. Add cipher to database config

Currently only Postgres and PGP is supported.

'pgsql' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => 'public',
    'sslmode' => 'prefer',
    'cipher' => 'pgp'
],

2. Usage

2.1 Models

Add the DatabaseEncryption Trait to your eloquent model.

<?php

namespace App;

use Anexia\LaravelEncryption\DatabaseEncryption;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;


class User extends Authenticatable
{
    use Notifiable, DatabaseEncryption;
    

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];
    

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    

    /**
     * @return array
     */
    protected static function getEncryptedFields()
    {
        return [
            'password'
        ];
    }
    

    /**
     * @return string
     */
    protected function getEncryptKey()
    {
        return 'thisismysupersecretencryptionkey';
    }
}

2.2 Updates

Just call the save() method on the model. Fields will be encrypted automatically.

2.3 Queries

Per default the encrypted properties will be replaced by their corresponding "_encrypted" value.

$user = User::find(1);

The above query will have the property "password_encrypted" and no "password" property.

2.3.1 Decrypted properties

Use the macro withDecryptKey for automatic decryption.

2.3.1 'select *' queries

$user = User::withDecryptKey('thisismysupersecretencryptionkey')->find(1);

In the example above $user will have two properties:

  • password: the decrypted password
  • password_encrypted: the encrypted value from the database
  • id
  • name
  • email
  • remember_token

2.3.2 Select certain fields

$user = User::->find(1, ['id']);

or

$user = User::withDecryptKey('thisismysupersecretencryptionkey')->find(1, ['id']);

In both examples above $user will only have one property:

  • id
$user = User::->find(1, ['id', 'password']);

In the example above $user will only have two properties:

  • id
  • password_encrypted: the encrypted value from the database
$user = User::withDecryptKey('thisismysupersecretencryptionkey')->find(1, ['id', 'password']);

In the example above $user will only have three properties:

  • id
  • password: the decrypted password
  • password_encrypted: the encrypted value from the database

2.3.3 Check for certain decoded values

Use the macro whereDecripted to run a "where field = value" query on an encrypted property.

$user = User::whereDecrypted('password', 'thisIsTheWantedPassword', 'thisismysupersecretencryptionkey')->first();

In the example above $user will be the first entry with the (decrypted) password 'thisIsTheWantedPassword'.

anexia/laravel-encryption 适用场景与选型建议

anexia/laravel-encryption 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.07k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 anexia/laravel-encryption 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 anexia/laravel-encryption 我们能提供哪些服务?
定制开发 / 二次开发

基于 anexia/laravel-encryption 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 24
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-10