1 | <?php namespace Wubbajack\Encryption; |
||
21 | class FileEncrypter |
||
22 | { |
||
23 | |||
24 | const CHUNK_BYTES = 8192; |
||
25 | |||
26 | /** |
||
27 | * The encryption key. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $key; |
||
32 | |||
33 | /** |
||
34 | * The algorithm used for encryption. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $cipher = MCRYPT_RIJNDAEL_128; |
||
39 | |||
40 | /** |
||
41 | * The mode used for encryption. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $mode = MCRYPT_MODE_CBC; |
||
46 | |||
47 | /** |
||
48 | * The block size of the cipher. |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $block = 16; |
||
53 | |||
54 | /** |
||
55 | * FileEncrypter constructor. |
||
56 | * |
||
57 | * @param $key |
||
58 | */ |
||
59 | 12 | public function __construct($key) |
|
63 | |||
64 | /** |
||
65 | * Set the encryption key. |
||
66 | * |
||
67 | * @param string $key |
||
68 | * @return void |
||
69 | */ |
||
70 | 12 | public function setKey($key) |
|
74 | |||
75 | /** |
||
76 | * Set the encryption cipher. |
||
77 | * |
||
78 | * @param string $cipher |
||
79 | * @return $this |
||
80 | */ |
||
81 | 12 | public function setCipher($cipher) |
|
88 | |||
89 | /** |
||
90 | * Set the encryption mode. |
||
91 | * |
||
92 | * @param string $mode |
||
93 | * @return $this |
||
94 | */ |
||
95 | 12 | public function setMode($mode) |
|
102 | |||
103 | /** |
||
104 | * Encrypts a file and returns the checksum of the encrypted file. |
||
105 | * You can use the checksum to verify integrity as this method of encryption (symmetrical) |
||
106 | * doesn't allow for easy integrity verification. |
||
107 | * |
||
108 | * It's not required but highly recommended as an attacker can shift bytes and thus changes the data |
||
109 | * on the encrypted file. |
||
110 | * |
||
111 | * @param string $source |
||
112 | * @param string $target |
||
113 | * @return EncryptedFile An encrypted file object containing information about the IV, checksum and padding |
||
114 | * @throws EncryptException |
||
115 | */ |
||
116 | 6 | public function encrypt($source, $target) |
|
134 | |||
135 | /** |
||
136 | * Decrypts the source file to a target file. The checksum is an optional parameter |
||
137 | * that can be used to verify integrity of the file some ciphers offer no integrity check of their own. |
||
138 | * |
||
139 | * It's an optional parameter but be warned, the file may have been tampered with by an attacker. |
||
140 | * |
||
141 | * @param EncryptedFile $encryptedFile |
||
142 | * @param string $target |
||
143 | * @return string Path to the target file |
||
144 | * @throws DecryptException |
||
145 | */ |
||
146 | 2 | public function decrypt(EncryptedFile $encryptedFile, $target) |
|
165 | |||
166 | /** |
||
167 | * Decrypts a file in a stream, performing the callback on each successive decrypted block. |
||
168 | * If the checksum is provided it checks it against the encrypted file for integrity. |
||
169 | * |
||
170 | * The callback can accept two arguments: |
||
171 | * - $data - A chunk of decrypted data |
||
172 | * - $stream - The resource stream that is decrypting |
||
173 | * |
||
174 | * @param EncryptedFile $encryptedFile |
||
175 | * @param \Closure $callback |
||
176 | * @throws DecryptException |
||
177 | */ |
||
178 | 2 | public function streamDecrypt(EncryptedFile $encryptedFile, \Closure $callback) |
|
203 | |||
204 | /** |
||
205 | * Encrypts the file |
||
206 | * |
||
207 | * @param string $source_file |
||
208 | * @param string $target_file |
||
209 | * @param string $iv |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | 6 | protected function encryptFile($source_file, $target_file, $iv) |
|
228 | |||
229 | /** |
||
230 | * Decrypts a source file into a target file |
||
231 | * |
||
232 | * @param string $source |
||
233 | * @param string $target |
||
234 | * @param string $iv |
||
235 | * @param int $padding |
||
236 | * |
||
237 | * @return void |
||
238 | */ |
||
239 | 2 | protected function decryptFile($source, $target, $iv, $padding = 0) |
|
252 | |||
253 | /** |
||
254 | * Creates a stream that encrypts when written to |
||
255 | * |
||
256 | * @param string $target |
||
257 | * @param array $options |
||
258 | * |
||
259 | * @return resource |
||
260 | * @throws EncryptException |
||
261 | */ |
||
262 | 6 | protected function createEncryptionStream($target, array $options) |
|
273 | |||
274 | /** |
||
275 | * Creates a stream that is decrypted when read from |
||
276 | * |
||
277 | * @param string $source |
||
278 | * @param array $options |
||
279 | * |
||
280 | * @return resource |
||
281 | */ |
||
282 | 4 | protected function createDecryptionStream($source, array $options) |
|
292 | |||
293 | /** |
||
294 | * Copies a source stream to a target stream. |
||
295 | * If the padding parameter is set it will remove said amount of bytes from the end of the file. |
||
296 | * |
||
297 | * This method does not use stream_copy_to_stream on purpose because this way we have more control |
||
298 | * over the process of moving data from one stream to another. |
||
299 | * |
||
300 | * @param resource $source |
||
301 | * @param resource $target |
||
302 | * @param null|int $padding |
||
303 | * |
||
304 | * @return void |
||
305 | */ |
||
306 | 6 | protected function copyStream($source, $target, $padding = null) |
|
320 | |||
321 | /** |
||
322 | * Returns the options for the stream filter |
||
323 | * @param null $iv If no IV is set, one will be created |
||
324 | * |
||
325 | * @return array Returns an array with 'mode','key' and 'iv' |
||
326 | */ |
||
327 | 6 | protected function getOptions($iv = null) |
|
339 | |||
340 | /** |
||
341 | * Get the IV size for the cipher. |
||
342 | * |
||
343 | * @return int |
||
344 | */ |
||
345 | 6 | protected function getIvSize() |
|
349 | |||
350 | /** |
||
351 | * Returns the encryption cipher for the stream filter |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | 6 | protected function getEncryptionFilterName() |
|
359 | |||
360 | /** |
||
361 | * Returns the decryption cipher for the stream filter |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | 4 | protected function getDecryptionFilterName() |
|
369 | |||
370 | /** |
||
371 | * Get the random data source available for the OS. |
||
372 | * |
||
373 | * @return int |
||
374 | */ |
||
375 | 6 | protected function getRandomizer() |
|
389 | |||
390 | /** |
||
391 | * Compares the given checksum with the actual file checksum. |
||
392 | * Returns true if they match, false if not |
||
393 | * |
||
394 | * @param string $file |
||
395 | * @param string $checksum |
||
396 | * @return bool |
||
397 | */ |
||
398 | 2 | protected function verifyChecksum($file, $checksum) |
|
402 | |||
403 | /** |
||
404 | * Update the block size for the current cipher and mode. |
||
405 | * |
||
406 | * @return void |
||
407 | */ |
||
408 | 12 | protected function updateBlockSize() |
|
412 | |||
413 | /** |
||
414 | * Calculates the padding that was added during encryption |
||
415 | * |
||
416 | * @param string $source Path the the source file |
||
417 | * @param string $target Path to the target file |
||
418 | * @return int |
||
419 | */ |
||
420 | 6 | protected function calculatePadding($source, $target) |
|
424 | |||
425 | /** |
||
426 | * Calculates the checksum of the file |
||
427 | * |
||
428 | * @param string $file |
||
429 | * @return string |
||
430 | */ |
||
431 | 6 | protected function calculateChecksum($file) |
|
435 | } |
||
436 |