Skip to content

Commit d09273a

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

File tree

5 files changed

+20
-27
lines changed

5 files changed

+20
-27
lines changed

src/FileEncryptor.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Rcsofttech85\FileHandler;
44

55
use Exception;
6-
use Rcsofttech85\FileHandler\DependencyInjection\ServiceContainer;
76
use Rcsofttech85\FileHandler\Exception\FileEncryptorException;
87
use Rcsofttech85\FileHandler\Exception\FileHandlerException;
98
use Rcsofttech85\FileHandler\Validator\FileValidatorTrait;
@@ -15,10 +14,6 @@ final class FileEncryptor
1514

1615
public const ENCRYPT_PASSWORD = 'ENCRYPT_PASSWORD';
1716

18-
public function __construct(private ServiceContainer $serviceContainer)
19-
{
20-
}
21-
2217
/**
2318
*
2419
* @throws FileEncryptorException
@@ -40,9 +35,8 @@ public function encryptFile(string $filename): bool
4035

4136
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
4237

43-
$container = $this->serviceContainer->getContainerBuilder();
4438

45-
$secret = $this->getParam($container, self::ENCRYPT_PASSWORD);
39+
$secret = $this->getParam(self::ENCRYPT_PASSWORD);
4640

4741
$key = hash('sha256', $secret, true);
4842

@@ -87,9 +81,8 @@ public function decryptFile(string $filename): bool
8781
$nonce = substr($bytes, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
8882
$ciphertext = substr($bytes, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
8983

90-
$container = $this->serviceContainer->getContainerBuilder();
9184

92-
$secret = $this->getParam($container, self::ENCRYPT_PASSWORD);
85+
$secret = $this->getParam(self::ENCRYPT_PASSWORD);
9386

9487
$key = hash('sha256', $secret, true);
9588

src/FileHashChecker.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(private readonly CsvFileHandler $csvFileHandler)
3535

3636
public function verifyHash(string $filename, string $algo = self::ALGO_256): bool
3737
{
38-
$storedHashesFile = $this->getParameter(self::STORED_HASH_FILE);
38+
$storedHashesFile = $this->getParam(self::STORED_HASH_FILE);
3939
$file = $this->csvFileHandler->searchInCsvFile(
4040
filename: $storedHashesFile,
4141
keyword: $filename,
@@ -62,8 +62,10 @@ public function verifyHash(string $filename, string $algo = self::ALGO_256): boo
6262
/**
6363
* @param string $filename
6464
* @param string $algo
65+
* @param string $env
6566
* @return string
66-
* @throws HashException|FileHandlerException
67+
* @throws FileHandlerException
68+
* @throws HashException
6769
*/
6870

6971
public function hashFile(
@@ -80,7 +82,7 @@ public function hashFile(
8082
throw new HashException('could not hash file');
8183
}
8284

83-
$storedHashesFile = $this->getParameter($env);
85+
$storedHashesFile = $this->getParam($env);
8486

8587

8688
$file = fopen($storedHashesFile, 'a+');
@@ -100,7 +102,7 @@ public function hashFile(
100102
if (!$filenameExists) {
101103
fputcsv($file, [$filename, $hash]);
102104
}
103-
} catch (FileHandlerException) {
105+
} catch (FileHandlerException $e) {
104106
fputcsv($file, [$filename, $hash]);
105107
} finally {
106108
fclose($file);

src/Validator/FileValidatorTrait.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function sanitize(string $filename): string
5555
*/
5656
private function isFileSafe(string $filename, string $envVariable): bool
5757
{
58-
$safeFile = $this->getParameter($envVariable);
58+
$safeFile = $this->getParam($envVariable);
5959

6060

6161
if ($safeFile !== $filename) {
@@ -69,31 +69,32 @@ private function isFileSafe(string $filename, string $envVariable): bool
6969
* @param string $filename
7070
* @param string $envVariable
7171
* @return bool
72+
* @throws FileHandlerException
7273
*/
7374
public function isFileRestricted(string $filename, string $envVariable): bool
7475
{
7576
return $this->isFileSafe($filename, $envVariable);
7677
}
7778

7879
/**
79-
* @param string $param
80-
* @return string
81-
* @throws FileHandlerException
80+
* @return ContainerBuilder
8281
*/
83-
private function getParameter(string $param): string
82+
private function getContainerBuilder(): ContainerBuilder
8483
{
85-
$container = (new ServiceContainer())->getContainerBuilder();
86-
return $this->getParam($container, $param);
84+
return (new ServiceContainer())->getContainerBuilder();
8785
}
8886

8987
/**
90-
* @param ContainerBuilder $container
9188
* @param string $parameter
89+
* @param ContainerBuilder|null $container
9290
* @return string
9391
* @throws FileHandlerException
9492
*/
95-
public function getParam(ContainerBuilder $container, string $parameter): string
93+
public function getParam(string $parameter, ContainerBuilder $container = null): string
9694
{
95+
if (null === $container) {
96+
$container = $this->getContainerBuilder();
97+
}
9798
$param = $container->getParameter($parameter);
9899
if (!is_string($param)) {
99100
throw new FileHandlerException("{$parameter} is not string type");

src/config/services.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
services:
2-
container:
3-
class: 'Rcsofttech85\FileHandler\DependencyInjection\ServiceContainer'
4-
52
file_handler:
63
class: 'Rcsofttech85\FileHandler\FileHandler'
74

85
file_encryptor:
96
class: 'Rcsofttech85\FileHandler\FileEncryptor'
10-
arguments: [ '@container' ]
7+
118

129

1310
temp_file_handler:

tests/unit/FileValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getParamMethodValidateForStringType(): void
5252
$container->setParameter('arr', []);
5353
$this->expectException(FileHandlerException::class);
5454
$this->expectExceptionMessage("arr is not string type");
55-
$this->getParam($container, 'arr');
55+
$this->getParam('arr', $container);
5656
}
5757

5858
#[Test]

0 commit comments

Comments
 (0)