Skip to content

Commit 9e0d6e4

Browse files
authored
load encrypt password from env (#51)
Signed-off-by: rahul <[email protected]>
1 parent 2f01d39 commit 9e0d6e4

File tree

3 files changed

+62
-44
lines changed

3 files changed

+62
-44
lines changed

src/FileEncryptor.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,20 @@
33
namespace Rcsofttech85\FileHandler;
44

55
use Exception;
6+
use Rcsofttech85\FileHandler\DependencyInjection\ServiceContainer;
67
use Rcsofttech85\FileHandler\Exception\FileEncryptorException;
78
use Rcsofttech85\FileHandler\Exception\FileHandlerException;
89
use Rcsofttech85\FileHandler\Validator\FileValidatorTrait;
9-
use SensitiveParameter;
1010
use SodiumException;
1111

12-
readonly class FileEncryptor
12+
final class FileEncryptor
1313
{
1414
use FileValidatorTrait;
1515

16-
/**
17-
* @param string $filename
18-
* @param string $secret
19-
*
20-
* @throws FileHandlerException
21-
*/
22-
public function __construct(
23-
private string $filename,
24-
#[SensitiveParameter] private string $secret
25-
) {
26-
$this->validateFileName($filename);
16+
public const ENCRYPT_PASSWORD = 'ENCRYPT_PASSWORD';
17+
18+
public function __construct(private ServiceContainer $serviceContainer)
19+
{
2720
}
2821

2922
/**
@@ -32,9 +25,10 @@ public function __construct(
3225
* @throws Exception
3326
*
3427
*/
35-
public function encryptFile(): bool
28+
public function encryptFile(string $filename): bool
3629
{
37-
$plainText = file_get_contents($this->filename);
30+
$this->validateFileName($filename);
31+
$plainText = file_get_contents($filename);
3832

3933
if (!$plainText) {
4034
throw new FileEncryptorException('File has no content');
@@ -46,15 +40,18 @@ public function encryptFile(): bool
4640

4741
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
4842

43+
$container = $this->serviceContainer->getContainerBuilder();
44+
45+
$secret = $this->getParam($container, self::ENCRYPT_PASSWORD);
4946

50-
$key = hash('sha256', $this->secret, true);
47+
$key = hash('sha256', $secret, true);
5148

5249

5350
$ciphertext = sodium_crypto_secretbox($plainText, $nonce, $key);
5451

5552
$output = bin2hex($nonce . $ciphertext);
5653

57-
$file = $this->openFileAndReturnResource($this->filename);
54+
$file = $this->openFileAndReturnResource($filename);
5855

5956
try {
6057
fwrite($file, $output);
@@ -71,9 +68,10 @@ public function encryptFile(): bool
7168
* @throws FileHandlerException
7269
* @throws SodiumException
7370
*/
74-
public function decryptFile(): bool
71+
public function decryptFile(string $filename): bool
7572
{
76-
$encryptedData = file_get_contents($this->filename);
73+
$this->validateFileName($filename);
74+
$encryptedData = file_get_contents($filename);
7775

7876
if (!$encryptedData) {
7977
throw new FileEncryptorException('File has no content');
@@ -83,22 +81,27 @@ public function decryptFile(): bool
8381
throw new FileEncryptorException('file is not encrypted');
8482
}
8583

84+
8685
$bytes = $this->convertHexToBin($encryptedData);
8786

8887
$nonce = substr($bytes, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
8988
$ciphertext = substr($bytes, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
9089

91-
$key = hash('sha256', $this->secret, true);
90+
$container = $this->serviceContainer->getContainerBuilder();
91+
92+
$secret = $this->getParam($container, self::ENCRYPT_PASSWORD);
93+
94+
$key = hash('sha256', $secret, true);
9295

9396
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
97+
$file = $this->openFileAndReturnResource($filename);
9498

9599
if (!$plaintext) {
100+
fwrite($file, $encryptedData);
96101
throw new FileEncryptorException('could not decrypt file');
97102
}
98103

99104

100-
$file = $this->openFileAndReturnResource($this->filename);
101-
102105
try {
103106
fwrite($file, $plaintext);
104107
} finally {

src/config/services.yaml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
parameters:
2-
filename: 'movie.csv'
3-
secret: 'password'
4-
5-
6-
7-
81
services:
2+
container:
3+
class: 'Rcsofttech85\FileHandler\DependencyInjection\ServiceContainer'
4+
95
file_handler:
106
class: 'Rcsofttech85\FileHandler\FileHandler'
117

128
file_encryptor:
139
class: 'Rcsofttech85\FileHandler\FileEncryptor'
14-
arguments: [ '%filename%','%secret%' ]
10+
arguments: [ '@container' ]
11+
1512

1613
temp_file_handler:
1714
class: 'Rcsofttech85\FileHandler\TempFileHandler'

tests/unit/FileEncryptorTest.php

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Base\BaseTest;
66
use PHPUnit\Framework\Attributes\Test;
77
use Rcsofttech85\FileHandler\Exception\FileEncryptorException;
8+
use Rcsofttech85\FileHandler\Exception\FileHandlerException;
89
use Rcsofttech85\FileHandler\FileEncryptor;
910
use SodiumException;
1011

@@ -29,28 +30,31 @@ protected function tearDown(): void
2930
/**
3031
* @return void
3132
* @throws FileEncryptorException
33+
* @throws FileHandlerException
3234
* @throws SodiumException
3335
*/
3436
#[Test]
3537
public function throwExceptionOnDecryptingNonEncryptedFile(): void
3638
{
3739
$this->expectException(FileEncryptorException::class);
3840
$this->expectExceptionMessage('file is not encrypted');
39-
$this->fileEncryptor->decryptFile();
41+
$this->fileEncryptor->decryptFile('movie.csv');
4042
}
4143

44+
4245
/**
4346
* @return void
4447
* @throws FileEncryptorException
4548
*/
4649
#[Test]
4750
public function canEncryptFile(): void
4851
{
49-
$isFileEncrypted = $this->fileEncryptor->encryptFile();
52+
$isFileEncrypted = $this->fileEncryptor->encryptFile('movie.csv');
5053

5154
$this->assertTrue($isFileEncrypted);
5255
}
5356

57+
5458
/**
5559
* @return void
5660
* @throws FileEncryptorException
@@ -60,9 +64,10 @@ public function throwExceptionIfAlreadyEncrypted(): void
6064
{
6165
$this->expectException(FileEncryptorException::class);
6266
$this->expectExceptionMessage('file is already encrypted');
63-
$this->fileEncryptor->encryptFile();
67+
$this->fileEncryptor->encryptFile('movie.csv');
6468
}
6569

70+
6671
/**
6772
* @return void
6873
* @throws FileEncryptorException
@@ -71,12 +76,12 @@ public function throwExceptionIfAlreadyEncrypted(): void
7176
public function throwExceptionIfFileHasNoContentWhileEncrypt(): void
7277
{
7378
file_put_contents("test", "");
74-
$file = new FileEncryptor('test', 'pass');
7579
$this->expectException(FileEncryptorException::class);
7680
$this->expectExceptionMessage('File has no content');
77-
$file->encryptFile();
81+
$this->fileEncryptor->encryptFile('test');
7882
}
7983

84+
8085
#[Test]
8186
public function throwExceptionIfCouldNotConvertHexToBin(): void
8287
{
@@ -89,42 +94,55 @@ public function throwExceptionIfCouldNotConvertHexToBin(): void
8994
* @return void
9095
* @throws FileEncryptorException
9196
* @throws SodiumException
97+
* @throws FileHandlerException
9298
*/
9399
#[Test]
94100
public function throwExceptionIfFileHasNoContent(): void
95101
{
96102
file_put_contents("test", "");
97-
$file = new FileEncryptor('test', 'pass');
98103
$this->expectException(FileEncryptorException::class);
99104
$this->expectExceptionMessage('File has no content');
100-
$file->decryptFile();
105+
$this->fileEncryptor->decryptFile('test');
101106
}
102107

108+
103109
/**
104110
* @return void
105111
* @throws FileEncryptorException
112+
* @throws FileHandlerException
106113
* @throws SodiumException
107114
*/
108-
109115
#[Test]
110116
public function throwExceptionIfDecryptionFails(): void
111117
{
112-
$fileEncryptor = new FileEncryptor('movie.csv', 'wrong');
113-
114-
$this->expectException(FileEncryptorException::class);
115-
$this->expectExceptionMessage('could not decrypt file');
116-
$fileEncryptor->decryptFile();
118+
$filePath = '.env';
119+
$originalContent = file_get_contents($filePath);
120+
if (!$originalContent) {
121+
$this->fail('file not found');
122+
}
123+
$password = $_ENV[FileEncryptor::ENCRYPT_PASSWORD];
124+
$updatedContent = str_replace($password, 'pass', $originalContent);
125+
126+
file_put_contents($filePath, $updatedContent);
127+
try {
128+
$this->expectException(FileEncryptorException::class);
129+
$this->expectExceptionMessage('could not decrypt file');
130+
$this->fileEncryptor->decryptFile('movie.csv');
131+
} finally {
132+
file_put_contents($filePath, $originalContent);
133+
}
117134
}
118135

119136
/**
120137
* @return void
121138
* @throws FileEncryptorException
139+
* @throws FileHandlerException
122140
* @throws SodiumException
123141
*/
124142
#[Test]
125143
public function canDecryptFile(): void
126144
{
127-
$isFileDecrypted = $this->fileEncryptor->decryptFile();
145+
$isFileDecrypted = $this->fileEncryptor->decryptFile('movie.csv');
128146

129147
$this->assertTrue($isFileDecrypted);
130148
}

0 commit comments

Comments
 (0)