Completed
Push — master ( 8055c8...3e2656 )
by Tony Karavasilev (Тони
16:45
created

FileEncryptionTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 15
c 1
b 0
f 0
dl 0
loc 82
ccs 0
cts 27
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decryptFile() 0 9 2
A encryptFile() 0 9 2
A validateFileNamePath() 0 13 6
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)) {
1 ignored issue
show
introduced by
The condition is_string($filename) is always true.
Loading history...
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