Skip to content

Commit

Permalink
Updated repo to Tiknil namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
gbalduzzi committed Jan 13, 2023
1 parent 6b2cde9 commit 695a3a3
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 48 deletions.
99 changes: 66 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
# File encryption / decryption in Laravel

[![Latest Version on Packagist](https://img.shields.io/packagist/v/soarecostin/file-vault.svg?style=flat-square)](https://packagist.org/packages/soarecostin/file-vault)
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/soarecostin/file-vault/master.svg?style=flat-square)](https://travis-ci.org/soarecostin/file-vault)
[![Quality Score](https://img.shields.io/scrutinizer/g/soarecostin/file-vault.svg?style=flat-square)](https://scrutinizer-ci.com/g/soarecostin/file-vault)
[![StyleCI](https://styleci.io/repos/221933072/shield)](https://styleci.io/repos/221933072)
[![Total Downloads](https://img.shields.io/packagist/dt/soarecostin/file-vault.svg?style=flat-square)](https://packagist.org/packages/soarecostin/file-vault)
## ⚠️ This is a fork of [soarecostin/file-vault](https://github.com/soarecostin/file-vault) ⚠️

With this package, you can encrypt and decrypt files of any size in your Laravel project. This package uses streams and [CBC encryption](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_(CBC)), encrypting / decrypting a segment of data at a time.
We forked the repository due to abandoned state of the project in order to fix some issues we were having:

- [Fix a S3 bug returning chunk with a different than expected chunk size](https://github.com/soarecostin/file-vault/pull/20)
- Add Laravel 9, PHP 8 and Flysystem v23 support

Refer to the original repo for the history of opened and closed issues

The namespace has been changed from `Soarecostin/FileVault` to `Tiknil/FileVault` to allow parallel usage of both versions

---

With this package, you can encrypt and decrypt files of any size in your Laravel project. This package uses streams and [CBC encryption](<https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_(CBC)>), encrypting / decrypting a segment of data at a time.

## Installation and usage

This package requires PHP 7.2 and Laravel 5.8 or higher.
This package requires PHP 7.2 and Laravel 5.8 or higher.

You can install the package via composer:
You can install the original package via composer:

```bash
composer require soarecostin/file-vault
```

or this fork by adding the reference to the github repo in your composer.json:

```php
"repositories": [
{
"type": "vcs",
"url": "https://github.com/tiknil/file-vault"
}
],
```

and then

```bash
composer require tiknil/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](https://medium.com/swlh/how-to-encrypt-large-files-in-laravel-293460836ded?source=friends_link&sk=976ab6e5d1cfb52e10c801fe0cb04fca)
- [Part 2: How to encrypt & upload large files to Amazon S3 in Laravel](https://medium.com/@soarecostin/how-to-encrypt-upload-large-files-to-amazon-s3-in-laravel-af88324a9aa?sk=a9a358a3892e898a60448d5314fb3dc0)

- [Part 1: How to encrypt large files in Laravel](https://medium.com/swlh/how-to-encrypt-large-files-in-laravel-293460836ded?source=friends_link&sk=976ab6e5d1cfb52e10c801fe0cb04fca)
- [Part 2: How to encrypt & upload large files to Amazon S3 in Laravel](https://medium.com/@soarecostin/how-to-encrypt-upload-large-files-to-amazon-s3-in-laravel-af88324a9aa?sk=a9a358a3892e898a60448d5314fb3dc0)

### 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:
Expand All @@ -37,7 +62,8 @@ php artisan vendor:publish --provider="SoareCostin\FileVault\FileVaultServicePro
```

This is the contents of the published file:
``` php

```php
return [
/*
* The default key used for all file encryption / decryption
Expand All @@ -59,41 +85,43 @@ return [
];
```


### 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.

``` php
```php
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.

``` php
```php
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`:
``` php

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

You can also specify a different `disk`, just as you would normally with the Laravel `Storage` facade:
``` php

```php
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`:
``` php

```php
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:
``` php

```php
// save the encrypted copy to file.txt.enc
FileVault::encryptCopy('file.txt');

Expand All @@ -105,40 +133,45 @@ FileVault::encryptCopy('file.txt', 'encrypted.txt');

The `decrypt` method will search for a file, decrypt it and save it in the same directory, while deleting the encrypted file.

``` php
```php
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.

``` php
```php
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`:
``` php

```php
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`:
``` php

```php
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:
``` php

```php
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`:
``` php

```php
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:
``` php

```php
// save the decrypted copy to file.txt while preserving file.txt.enc
FileVault::decryptCopy('file.txt.enc');

Expand All @@ -150,7 +183,7 @@ FileVault::decryptCopy('file.txt.enc', 'decrypted.txt');

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](https://laravel.com/docs/6.x/responses#file-downloads) (available since 5.6) in order to generate a downloadable response:

``` php
```php
return response()->streamDownload(function () {
FileVault::streamDecrypt('file.txt')
}, 'laravel-readme.md');
Expand All @@ -160,23 +193,23 @@ return response()->streamDownload(function () {

You may need to use different keys to encrypt your files. You can explicitly specify the key used for encryption using the `key` method.

``` php
```php
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:

``` php
```php
$encryptionKey = FileVault::generateKey();
```

## Testing

Run the tests with:

``` bash
```bash
composer test
```

Expand All @@ -194,8 +227,8 @@ If you discover any security related issues, please email [email protected]

## Credits

- [Costin Soare](https://github.com/soarecostin)
- [All Contributors](../../contributors)
- [Costin Soare](https://github.com/soarecostin)
- [All Contributors](../../contributors)

## License

Expand Down
19 changes: 12 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "soarecostin/file-vault",
"name": "tiknil/file-vault",
"description": "",
"keywords": [
"laravel",
Expand All @@ -12,14 +12,19 @@
"file",
"file-vault"
],
"homepage": "https://github.com/soarecostin/file-vault",
"homepage": "https://github.com/tiknil/file-vault",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Costin Soare",
"email": "[email protected]",
"role": "Developer"
"role": "Original Developer"
},
{
"name": "Giorgio Balduzzi",
"email": "[email protected]",
"role": "Fork Developer"
}
],
"require": {
Expand All @@ -32,12 +37,12 @@
},
"autoload": {
"psr-4": {
"SoareCostin\\FileVault\\": "src"
"Tiknil\\FileVault\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"SoareCostin\\FileVault\\Tests\\": "tests"
"Tiknil\\FileVault\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -50,10 +55,10 @@
"extra": {
"laravel": {
"providers": [
"SoareCostin\\FileVault\\FileVaultServiceProvider"
"Tiknil\\FileVault\\FileVaultServiceProvider"
],
"aliases": {
"FileVault": "SoareCostin\\FileVault\\Facades\\FileVault"
"FileVault": "Tiknil\\FileVault\\Facades\\FileVault"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Facades/FileVault.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SoareCostin\FileVault\Facades;
namespace Tiknil\FileVault\Facades;

use Illuminate\Support\Facades\Facade;

Expand All @@ -12,7 +12,7 @@
* @method static mixed decrypt(string $sourceFile, string $destFile = null, $deleteSource = true)
* @method static mixed decryptCopy(string $sourceFile, string $destFile = null)
*
* @see \SoareCostin\FileVault\FileVault
* @see \Tiknil\FileVault\FileVault
*/
class FileVault extends Facade
{
Expand Down
2 changes: 1 addition & 1 deletion src/FileEncrypter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SoareCostin\FileVault;
namespace Tiknil\FileVault;

use Exception;
use Illuminate\Support\Str;
Expand Down
2 changes: 1 addition & 1 deletion src/FileVault.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SoareCostin\FileVault;
namespace Tiknil\FileVault;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
Expand Down
2 changes: 1 addition & 1 deletion src/FileVaultServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SoareCostin\FileVault;
namespace Tiknil\FileVault;

use Illuminate\Support\ServiceProvider;

Expand Down
6 changes: 3 additions & 3 deletions tests/FileVaultTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace SoareCostin\FileVault\Tests;
namespace Tiknil\FileVault\Tests;

use Illuminate\Support\Facades\Storage;
use Orchestra\Testbench\TestCase;
use SoareCostin\FileVault\Facades\FileVault;
use SoareCostin\FileVault\FileVaultServiceProvider;
use Tiknil\FileVault\Facades\FileVault;
use Tiknil\FileVault\FileVaultServiceProvider;

class FileVaultTest extends TestCase
{
Expand Down

0 comments on commit 695a3a3

Please sign in to comment.