soarecostin/file-vault
Composer 安装命令:
composer require soarecostin/file-vault
包简介
README 文档
README
With this package, you can encrypt and decrypt files of any size in your Laravel project. This package uses streams and CBC encryption, encrypting / decrypting a segment of data at a time.
Installation and usage
This package requires PHP 7.2 and Laravel 5.8 or higher.
You can install the package via composer:
composer require soarecostin/file-vault
Usage
Tutorials
For a detailed description of how to encrypt files in Laravel using this package, please see the following articles:
- Part 1: How to encrypt large files in Laravel
- Part 2: How to encrypt & upload large files to Amazon S3 in Laravel
Description
This package will automatically register a facade called FileVault. The FileVault facade is using the Laravel Storage and will allow you to specify a disk, just as you would normally do when working with Laravel Storage. All file names/paths that you will have to pass into the package encrypt/decrypt functions are relative to the disk root folder. By default, the local disk is used, but you can either specify a different disk each time you call one of FileVault methods, or you can set the default disk to something else, by publishing this package's config file.
If you want to change the default disk or change the key/cipher used for encryption, you can publish the config file:
php artisan vendor:publish --provider="SoareCostin\FileVault\FileVaultServiceProvider"
This is the contents of the published file:
return [ /* * The default key used for all file encryption / decryption * This package will look for a FILE_VAULT_KEY in your env file * If no FILE_VAULT_KEY is found, then it will use your Laravel APP_KEY */ 'key' => env('FILE_VAULT_KEY', env('APP_KEY')), /* * The cipher used for encryption. * Supported options are AES-128-CBC and AES-256-CBC */ 'cipher' => 'AES-256-CBC', /* * The Storage disk used by default to locate your files. */ 'disk' => 'local', ];
Encrypting a file
The encrypt method will search for a file, encrypt it and save it in the same directory, while deleting the original file.
public function encrypt(string $sourceFile, string $destFile = null, $deleteSource = true)
The encryptCopy method will search for a file, encrypt it and save it in the same directory, while preserving the original file.
public function encryptCopy(string $sourceFile, string $destFile = null)
Examples:
The following example will search for file.txt into the local disk, save the encrypted file as file.txt.enc and delete the original file.txt:
FileVault::encrypt('file.txt');
You can also specify a different disk, just as you would normally with the Laravel Storage facade:
FileVault::disk('s3')->encrypt('file.txt');
You can also specify a different name for the encrypted file by passing in a second parameter. The following example will search for file.txt into the local disk, save the encrypted file as encrypted.txt and delete the original file.txt:
FileVault::encrypt('file.txt', 'encrypted.txt');
The following examples both achive the same results as above, with the only difference that the original file is not deleted:
// save the encrypted copy to file.txt.enc FileVault::encryptCopy('file.txt'); // or save the encrypted copy with a different name FileVault::encryptCopy('file.txt', 'encrypted.txt');
Decrypting a file
The decrypt method will search for a file, decrypt it and save it in the same directory, while deleting the encrypted file.
public function decrypt(string $sourceFile, string $destFile = null, $deleteSource = true)
The decryptCopy method will search for a file, decrypt it and save it in the same directory, while preserving the encrypted file.
public function decryptCopy(string $sourceFile, string $destFile = null)
Examples:
The following example will search for file.txt.enc into the local disk, save the decrypted file as file.txt and delete the encrypted file file.txt.enc:
FileVault::decrypt('file.txt.enc');
If the file that needs to be decrypted doesn't end with the .enc extension, the decrypted file will have the .dec extention. The following example will search for encrypted.txt into the local disk, save the decrypted file as encrypted.txt.dec and delete the encrypted file encrypted.txt:
FileVault::decrypt('encrypted.txt');
As with the encryption, you can also specify a different disk, just as you would normally with the Laravel Storage facade:
FileVault::disk('s3')->decrypt('file.txt.enc');
You can also specify a different name for the decrypted file by passing in a second parameter. The following example will search for encrypted.txt into the local disk, save the decrypted file as decrypted.txt and delete the original encrypted.txt:
FileVault::decrypt('encrypted.txt', 'decrypted.txt');
The following examples both achive the same results as above, with the only difference that the original (encrypted) file is not deleted:
// save the decrypted copy to file.txt while preserving file.txt.enc FileVault::decryptCopy('file.txt.enc'); // or save the decrypted copy with a different name, while preserving the file.txt.enc FileVault::decryptCopy('file.txt.enc', 'decrypted.txt');
Streaming a decrypted file
Sometimes you will only want to allow users to download the decrypted file, but you don't need to store the actual decrypted file. For this, you can use the streamDecrypt function that will decrypt the file and will write it to the php://output stream. You can use the Laravel streamDownload method (available since 5.6) in order to generate a downloadable response:
return response()->streamDownload(function () { FileVault::streamDecrypt('file.txt') }, 'laravel-readme.md');
Using a different key for each file
You may need to use different keys to encrypt your files. You can explicitly specify the key used for encryption using the key method.
FileVault::key($encryptionKey)->encrypt('file.txt');
Please note that the encryption key must be 16 bytes long for the AES-128-CBC cipher and 32 bytes long for the AES-256-CBC cipher.
You can generate a key with the correct length (based on the cipher specified in the config file) by using the generateKey method:
$encryptionKey = FileVault::generateKey();
Testing
Run the tests with:
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email soarecostin@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.
soarecostin/file-vault 适用场景与选型建议
soarecostin/file-vault 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 196.81k 次下载、GitHub Stars 达 191, 最近一次更新时间为 2019 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「file」 「encryption」 「laravel」 「encrypt」 「decryption」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 soarecostin/file-vault 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 soarecostin/file-vault 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 soarecostin/file-vault 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
A simple PHP class to encrypt a string and decrypt an encrypted string
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Encryption with AES-256 and HMAC-SHA256
Allows installation of Laravel where the PHP Mcrypt extension is not available. Provides encryption using OpenSSL, or by disabling encryption entierly.
High-level cryptographic primitives and security utilities for Maatify systems including password hashing, reversible encryption, HKDF key derivation, and key rotation.
统计信息
- 总下载量: 196.81k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 192
- 点击次数: 33
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-17