|
1
|
|
|
<?php namespace Wubbajack\Encryption; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Encrypted File class |
|
5
|
|
|
* |
|
6
|
|
|
* Contains information about an encrypted file: |
|
7
|
|
|
* - The IV used for encryption |
|
8
|
|
|
* - The amount of padding encryption has added to the file |
|
9
|
|
|
* - A SHA1 checksum of the original file |
|
10
|
|
|
* - A reference to the encrypted file, using \SplFileInfo |
|
11
|
|
|
* |
|
12
|
|
|
* @author Chris Stolk <[email protected]> |
|
13
|
|
|
* @package Wubbajack\Encryption |
|
14
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
|
15
|
|
|
* @since 0.0.1 |
|
16
|
|
|
*/ |
|
17
|
|
|
class EncryptedFile implements \Serializable |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $iv; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $checksum; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \SplFileInfo |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $file; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $padding = 0; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* EncryptedFile constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $iv |
|
43
|
|
|
* @param string $checksum |
|
44
|
|
|
* @param \SplFileInfo $file |
|
45
|
|
|
*/ |
|
46
|
10 |
|
public function __construct($iv, $checksum, \SplFileInfo $file) |
|
47
|
|
|
{ |
|
48
|
10 |
|
$this->iv = $iv; |
|
49
|
10 |
|
$this->file = $file; |
|
50
|
10 |
|
$this->checksum = $checksum; |
|
51
|
10 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Creates a new encrypted file object |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $iv |
|
57
|
|
|
* @param string $checksum |
|
58
|
|
|
* @param int $padding |
|
59
|
|
|
* @param \SplFileInfo|string $file |
|
60
|
|
|
* |
|
61
|
|
|
* @return static |
|
62
|
|
|
*/ |
|
63
|
10 |
|
public static function create($iv, $checksum, $padding, $file) |
|
64
|
|
|
{ |
|
65
|
10 |
|
if (!($file instanceof \SplFileInfo)) { |
|
66
|
10 |
|
if (!file_exists($file)) { |
|
67
|
2 |
|
throw new \RuntimeException('Unable to create encrypted file object, file doesn\'t exist'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
$file = new \SplFileInfo($file); |
|
71
|
5 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Returns the encrypted file |
|
74
|
10 |
|
$encryptedFile = new static($iv, $checksum, $file); |
|
75
|
10 |
|
return $encryptedFile->setPadding($padding); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns the IV |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
6 |
|
public function getIv() |
|
84
|
|
|
{ |
|
85
|
6 |
|
return $this->iv; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the checksum of the encrypted file |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
6 |
|
public function getChecksum() |
|
94
|
|
|
{ |
|
95
|
6 |
|
return $this->checksum; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns the encrypted file |
|
100
|
|
|
* |
|
101
|
|
|
* @return \SplFileInfo |
|
102
|
|
|
*/ |
|
103
|
6 |
|
public function getFile() |
|
104
|
|
|
{ |
|
105
|
6 |
|
return $this->file; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the amount of bytes that have been padded |
|
110
|
|
|
* |
|
111
|
|
|
* @return int |
|
112
|
|
|
*/ |
|
113
|
6 |
|
public function getPadding() |
|
114
|
|
|
{ |
|
115
|
6 |
|
return $this->padding; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Sets the padding |
|
120
|
|
|
* |
|
121
|
|
|
* @param int$padding |
|
122
|
|
|
* @return $this |
|
123
|
|
|
*/ |
|
124
|
10 |
|
public function setPadding($padding) |
|
125
|
|
|
{ |
|
126
|
10 |
|
$this->padding = (int)$padding; |
|
127
|
|
|
|
|
128
|
10 |
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* String representation of object |
|
133
|
|
|
* |
|
134
|
|
|
* @link http://php.net/manual/en/serializable.serialize.php |
|
135
|
|
|
* @return string the string representation of the object or null |
|
136
|
|
|
* @since 5.1.0 |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function serialize() |
|
139
|
|
|
{ |
|
140
|
2 |
|
return serialize([ |
|
141
|
2 |
|
'iv' => $this->iv, |
|
142
|
2 |
|
'checksum' => $this->checksum, |
|
143
|
2 |
|
'padding' => $this->padding, |
|
144
|
2 |
|
'file' => $this->file instanceof \SplFileInfo ? $this->file->getRealPath() : null |
|
145
|
1 |
|
]); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Constructs the object |
|
150
|
|
|
* |
|
151
|
|
|
* @link http://php.net/manual/en/serializable.unserialize.php |
|
152
|
|
|
* @param string $serialized <p> |
|
153
|
|
|
* The string representation of the object. |
|
154
|
|
|
* </p> |
|
155
|
|
|
* @return void |
|
156
|
|
|
* @since 5.1.0 |
|
157
|
|
|
*/ |
|
158
|
2 |
|
public function unserialize($serialized) |
|
159
|
|
|
{ |
|
160
|
2 |
|
$data = unserialize($serialized); |
|
161
|
|
|
|
|
162
|
2 |
|
$this->iv = $data['iv']; |
|
163
|
2 |
|
$this->checksum = $data['checksum']; |
|
164
|
2 |
|
$this->padding = $data['padding']; |
|
165
|
|
|
|
|
166
|
2 |
|
if ($data['file']) { |
|
167
|
2 |
|
$this->file = new \SplFileInfo($data['file']); |
|
168
|
1 |
|
} |
|
169
|
2 |
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|