alejandro-ap00/file-vault

一个用于加密和解密任意大小文件的Laravel包

v1.0.0 2024-05-03 15:55 UTC

This package is auto-updated.

Last update: 2024-09-08 14:10:40 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

使用此包,您可以在Laravel项目中加密和解密任意大小的文件。此包使用流和CBC加密,一次加密/解密一段数据。

分支

此包是从soarecostin/file-vault分叉而来,以添加对Laravel 10和11的支持。

安装

您可以通过composer安装此包

composer require alejandro-ap00/file-vault

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="file-vault-config"

这是发布配置文件的内容

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',
];

使用方法

教程

有关如何使用此包在Laravel中加密文件的详细说明,请参阅以下文章

描述

此包将自动注册一个名为FileVault的外观。该FileVault外观使用Laravel的Storage,并允许您指定一个disk,就像您在处理Laravel Storage时那样。您需要传递给包加密/解密函数的所有文件名/路径都是相对于磁盘根文件夹的。默认情况下使用的是local磁盘,但您可以在每次调用FileVault方法时指定不同的磁盘,或者通过发布此包的配置文件来设置默认磁盘。

如果您想更改默认的disk或更改用于加密的key/cipher,您可以发布配置文件

php artisan vendor:publish --provider="Brainstud\FileVault\FileVaultServiceProvider"

这是发布文件的内容

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',
];

加密文件

encrypt方法将搜索一个文件,加密它并将其保存到同一目录中,同时删除原始文件。

public function encrypt(string $sourceFile, string $destFile = null, $deleteSource = true)

encryptCopy方法将搜索一个文件,加密它并将其保存到同一目录中,同时保留原始文件。

public function encryptCopy(string $sourceFile, string $destFile = null)

示例

以下示例将在local磁盘上搜索file.txt,将加密文件保存为file.txt.enc,并删除原始的file.txt

FileVault::encrypt('file.txt');

您也可以指定不同的disk,就像您通常使用Laravel Storage外观那样

FileVault::disk('s3')->encrypt('file.txt');

您也可以通过传递第二个参数来指定加密文件的名称。以下示例将在local磁盘上搜索file.txt,将加密文件保存为encrypted.txt,并删除原始的file.txt

FileVault::encrypt('file.txt', 'encrypted.txt');

以下示例都实现了上述相同的结果,唯一的区别是原始文件没有被删除

// 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');

解密文件

decrypt方法将搜索一个文件,解密它并将其保存到同一目录中,同时删除加密文件。

public function decrypt(string $sourceFile, string $destFile = null, $deleteSource = true)

decryptCopy方法将搜索一个文件,解密它并将其保存到同一目录中,同时保留加密文件。

public function decryptCopy(string $sourceFile, string $destFile = null)

示例

以下示例将在本地磁盘搜索file.txt.enc文件,将解密后的文件保存为file.txt,并删除加密文件file.txt.enc

FileVault::decrypt('file.txt.enc');

如果需要解密的文件不以.enc扩展名结尾,解密后的文件将具有.dec扩展名。以下示例将在本地磁盘搜索encrypted.txt文件,将解密后的文件保存为encrypted.txt.dec,并删除加密文件encrypted.txt

FileVault::decrypt('encrypted.txt');

与加密类似,您也可以指定不同的disk,就像您通常使用Laravel Storage外观一样

FileVault::disk('s3')->decrypt('file.txt.enc');

您可以通过传入第二个参数来指定解密文件的另一个名称。以下示例将在本地磁盘搜索encrypted.txt文件,将解密后的文件保存为decrypted.txt,并删除原始文件encrypted.txt

FileVault::decrypt('encrypted.txt', 'decrypted.txt');

以下示例都实现了上述相同的结果,唯一的区别是原始(加密)文件没有被删除

// 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');

流式传输解密文件

有时您可能只想允许用户下载解密文件,但不需要存储实际的解密文件。为此,您可以使用streamDecrypt函数,该函数将解密文件并将其写入到php://output流。您可以使用Laravel streamDownload方法(自5.6版以来可用)来生成可下载的响应

return response()->streamDownload(function () {
    FileVault::streamDecrypt('file.txt')
}, 'laravel-readme.md');

为每个文件使用不同的密钥

您可能需要使用不同的密钥来加密您的文件。您可以使用key方法显式指定用于加密的密钥。

FileVault::key($encryptionKey)->encrypt('file.txt');

请注意,加密密钥的长度必须为16字节(用于AES-128-CBC加密),或32字节(用于AES-256-CBC加密)。

您可以使用generateKey方法生成正确长度的密钥(基于配置文件中指定的加密方式)

$encryptionKey = FileVault::generateKey();
$fileVault = new AlejandroAPorras\FileVault();
echo $fileVault->echoPhrase('Hello, AlejandroAPorras!');

测试

composer test

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请参阅我们的安全策略以了解如何报告安全漏洞。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。