Skip to content

Commit ff149e0

Browse files
authored
Update README.md
1 parent 59bee3f commit ff149e0

File tree

1 file changed

+102
-9
lines changed

1 file changed

+102
-9
lines changed

README.md

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,123 @@ composer require soarecostin/file-vault
2222

2323
## Usage
2424

25-
This package will automatically register a facade called `FileVault`.
25+
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.
26+
27+
If you want to change the default `disk` or change the `key`/`cipher` used for encryption, you can publish the config file:
28+
29+
```
30+
php artisan vendor:publish --provider="SoareCostin\FileVault\FileVaultServiceProvider"
31+
```
32+
33+
This is the contents of the published file:
34+
``` php
35+
return [
36+
/*
37+
* The default key used for all file encryption / decryption
38+
* This package will look for a FILE_VAULT_KEY in your env file
39+
* If no FILE_VAULT_KEY is found, then it will use your Laravel APP_KEY
40+
*/
41+
'key' => env('FILE_VAULT_KEY', env('APP_KEY')),
42+
43+
/*
44+
* The cipher used for encryption.
45+
* Supported options are AES-128-CBC and AES-256-CBC
46+
*/
47+
'cipher' => 'AES-256-CBC',
48+
49+
/*
50+
* The Storage disk used by default to locate your files.
51+
*/
52+
'disk' => 'local',
53+
];
54+
```
55+
2656

2757
### Encrypting a file
2858

29-
The `encrypt` method will search for a file, encrypt it and save it in the same directory.
59+
The `encrypt` method will search for a file, encrypt it and save it in the same directory, while deleting the original file.
3060

3161
``` php
3262
public function encrypt(string $sourceFile, string $destFile = null, $deleteSource = true)
3363
```
3464

65+
The `encryptCopy` method will search for a file, encrypt it and save it in the same directory, while preserving the original file.
66+
67+
``` php
68+
public function encryptCopy(string $sourceFile, string $destFile = null)
69+
```
70+
71+
3572
#### Examples:
3673

3774
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`:
3875
``` php
39-
FileVault::encrypt("file.txt");
76+
FileVault::encrypt('file.txt');
4077
```
4178

4279
You can also specify a different `disk`, just as you would normally with the Laravel `Storage` facade:
4380
``` php
44-
FileVault::disk('s3')->encrypt("file.txt");
81+
FileVault::disk('s3')->encrypt('file.txt');
4582
```
4683

47-
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`:
84+
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`:
4885
``` php
49-
FileVault::encrypt("file.txt", "encrypted.txt");
86+
FileVault::encrypt('file.txt', 'encrypted.txt');
5087
```
5188

5289
The following examples both achive the same results as above, with the only difference that the original file is not deleted:
5390
``` php
5491
// save the encrypted copy to file.txt.enc
55-
FileVault::encryptCopy("file.txt");
92+
FileVault::encryptCopy('file.txt');
5693

5794
// or save the encrypted copy with a different name
58-
FileVault::encryptCopy("file.txt", "encrypted.txt");
95+
FileVault::encryptCopy('file.txt', 'encrypted.txt');
5996
```
6097

6198
### Decrypting a file
6299

63-
The `decrypt` method will search for a file, decrypt it and save it in the same directory
100+
The `decrypt` method will search for a file, decrypt it and save it in the same directory, while deleting the encrypted file.
101+
102+
``` php
103+
public function decrypt(string $sourceFile, string $destFile = null, $deleteSource = true)
104+
```
105+
106+
The `decryptCopy` method will search for a file, decrypt it and save it in the same directory, while preserving the encrypted file.
107+
108+
``` php
109+
public function decryptCopy(string $sourceFile, string $destFile = null)
110+
```
111+
112+
#### Examples:
64113

114+
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`:
115+
``` php
116+
FileVault::decrypt('file.txt.enc');
117+
```
118+
119+
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`:
120+
``` php
121+
FileVault::decrypt('encrypted.txt');
122+
```
123+
124+
As with the encryption, you can also specify a different `disk`, just as you would normally with the Laravel `Storage` facade:
125+
``` php
126+
FileVault::disk('s3')->decrypt('file.txt.enc');
127+
```
128+
129+
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`:
130+
``` php
131+
FileVault::decrypt('encrypted.txt', 'decrypted.txt');
132+
```
133+
134+
The following examples both achive the same results as above, with the only difference that the original (encrypted) file is not deleted:
135+
``` php
136+
// save the decrypted copy to file.txt while preserving file.txt.enc
137+
FileVault::decryptCopy('file.txt.enc');
138+
139+
// or save the decrypted copy with a different name, while preserving the file.txt.enc
140+
FileVault::decryptCopy('file.txt.enc', 'decrypted.txt');
141+
```
65142

66143
### Streaming a decrypted file
67144

@@ -73,6 +150,22 @@ return response()->streamDownload(function () {
73150
}, 'laravel-readme.md');
74151
```
75152

153+
### Using a different key for each file
154+
155+
You may need to use different keys to encrypt your files. You can explicitly specify the key used for encryption using the `key` method.
156+
157+
``` php
158+
FileVault::key($encryptionKey)->encrypt('file.txt');
159+
```
160+
161+
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.
162+
163+
You can generate a key with the correct length (based on the cipher specified in the config file) by using the `generateKey` method:
164+
165+
``` php
166+
$encryptionKey = FileVault::generateKey();
167+
```
168+
76169
## Testing
77170

78171
Run the tests with:

0 commit comments

Comments
 (0)