|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Trait implementation of file encryption/decryption for symmetric encryption algorithms. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageEncryption; |
|
8
|
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\MessageEncryption\FileEncryptionInterface as FileEncryptionSpecification; |
|
10
|
|
|
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractSymmetricEncryptionAlgorithm as AnyEncryptionAlgorithm; |
|
11
|
|
|
use \CryptoManana\Core\StringBuilder as StringBuilder; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Trait FileEncryptionTrait - Reusable implementation of `FileEncryptionInterface`. |
|
15
|
|
|
* |
|
16
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\FileEncryptionInterface The abstract specification. |
|
17
|
|
|
* |
|
18
|
|
|
* @package CryptoManana\Core\Traits\MessageEncryption |
|
19
|
|
|
* |
|
20
|
|
|
* @mixin FileEncryptionSpecification |
|
21
|
|
|
* @mixin AnyEncryptionAlgorithm |
|
22
|
|
|
*/ |
|
23
|
|
|
trait FileEncryptionTrait |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Internal method for location and filename validation. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $filename The filename and location. |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \Exception Validation errors. |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function validateFileNamePath($filename) |
|
33
|
|
|
{ |
|
34
|
|
|
$filename = StringBuilder::stringReplace("\0", '', $filename); // (ASCII 0 (0x00)) |
|
35
|
|
|
$filename = realpath($filename); // Path traversal escape and absolute path fetching |
|
36
|
|
|
|
|
37
|
|
|
// Clear path cache |
|
38
|
|
|
if (!empty($filename)) { |
|
39
|
|
|
clearstatcache(true, $filename); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Check if path is valid and the file is readable |
|
43
|
|
|
if ($filename === false || !file_exists($filename) || !is_readable($filename) || !is_file($filename)) { |
|
44
|
|
|
throw new \RuntimeException('File is not found or can not be accessed.'); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Encrypts the content of a given plain file. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $filename The full path and name of the file for encryption. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string The encrypted file content. |
|
54
|
|
|
* @throws \Exception Validation errors. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function encryptFile($filename) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!is_string($filename)) { |
|
|
|
|
|
|
59
|
|
|
throw new \InvalidArgumentException('The file path must be of type string.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->validateFileNamePath($filename); |
|
63
|
|
|
|
|
64
|
|
|
return $this->encryptData(file_get_contents($filename)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Decrypts the content of a given encrypted file. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $filename The full path and name of the file for encryption. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string The decrypted file content. |
|
73
|
|
|
* @throws \Exception Validation errors. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function decryptFile($filename) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!is_string($filename)) { |
|
78
|
|
|
throw new \InvalidArgumentException('The file path must be of type string.'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->validateFileNamePath($filename); |
|
82
|
|
|
|
|
83
|
|
|
return $this->decryptData(file_get_contents($filename)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Encrypts the given plain data. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $plainData The plain input string. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string The cipher/encrypted data. |
|
92
|
|
|
* @throws \Exception Validation errors. |
|
93
|
|
|
*/ |
|
94
|
|
|
abstract public function encryptData($plainData); |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Decrypts the given cipher data. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $cipherData The encrypted input string. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string The decrypted/plain data. |
|
102
|
|
|
* @throws \Exception Validation errors. |
|
103
|
|
|
*/ |
|
104
|
|
|
abstract public function decryptData($cipherData); |
|
105
|
|
|
} |
|
106
|
|
|
|