1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MesCryptoBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mes\Security\CryptoBundle; |
13
|
|
|
|
14
|
|
|
use Defuse\Crypto\Exception\CryptoException as BaseCryptoException; |
15
|
|
|
use Defuse\Crypto\Exception\EnvironmentIsBrokenException; |
16
|
|
|
use Mes\Security\CryptoBundle\Exception\CryptoException; |
17
|
|
|
use Mes\Security\CryptoBundle\Model\KeyInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class EncryptionWrapper. |
21
|
|
|
*/ |
22
|
|
|
final class EncryptionWrapper implements EncryptionInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var EncryptionInterface |
26
|
|
|
*/ |
27
|
|
|
private $encryption; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* EncryptionWrapper constructor. |
31
|
|
|
* |
32
|
|
|
* @param EncryptionInterface $encryption |
33
|
|
|
*/ |
34
|
24 |
|
public function __construct(EncryptionInterface $encryption) |
35
|
|
|
{ |
36
|
24 |
|
$this->encryption = $encryption; |
37
|
24 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
* |
42
|
|
|
* @throws CryptoException |
43
|
|
|
*/ |
44
|
2 |
|
public function encryptWithKey($plaintext, KeyInterface $key) |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
2 |
|
return $this->encryption->encryptWithKey($plaintext, $key); |
48
|
1 |
|
} catch (EnvironmentIsBrokenException $ex) { |
49
|
1 |
|
throw new CryptoException($ex->getMessage()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
* |
56
|
|
|
* @throws CryptoException |
57
|
|
|
*/ |
58
|
2 |
|
public function decryptWithKey($ciphertext, KeyInterface $key) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
2 |
|
return $this->encryption->decryptWithKey($ciphertext, $key); |
62
|
1 |
|
} catch (BaseCryptoException $ex) { |
63
|
1 |
|
throw new CryptoException($ex->getMessage()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @throws CryptoException |
71
|
|
|
*/ |
72
|
2 |
|
public function encryptFileWithKey($inputFilename, $outputFilename, KeyInterface $key) |
73
|
|
|
{ |
74
|
|
|
try { |
75
|
2 |
|
$this->encryption->encryptFileWithKey($inputFilename, $outputFilename, $key); |
76
|
2 |
|
} catch (BaseCryptoException $ex) { |
77
|
1 |
|
throw new CryptoException($ex->getMessage()); |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
* |
84
|
|
|
* @throws CryptoException |
85
|
|
|
*/ |
86
|
2 |
|
public function decryptFileWithKey($inputFilename, $outputFilename, KeyInterface $key) |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
2 |
|
$this->encryption->decryptFileWithKey($inputFilename, $outputFilename, $key); |
90
|
2 |
|
} catch (BaseCryptoException $ex) { |
91
|
1 |
|
throw new CryptoException($ex->getMessage()); |
92
|
|
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
* |
98
|
|
|
* @throws CryptoException |
99
|
|
|
*/ |
100
|
2 |
|
public function encryptWithPassword($plaintext, $password) |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
2 |
|
return $this->encryption->encryptWithPassword($plaintext, $password); |
104
|
1 |
|
} catch (EnvironmentIsBrokenException $ex) { |
105
|
1 |
|
throw new CryptoException($ex->getMessage()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
* |
112
|
|
|
* @throws CryptoException |
113
|
|
|
*/ |
114
|
2 |
|
public function decryptWithPassword($ciphertext, $password) |
115
|
|
|
{ |
116
|
|
|
try { |
117
|
2 |
|
return $this->encryption->decryptWithPassword($ciphertext, $password); |
118
|
1 |
|
} catch (BaseCryptoException $ex) { |
119
|
1 |
|
throw new CryptoException($ex->getMessage()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
* |
126
|
|
|
* @throws CryptoException |
127
|
|
|
*/ |
128
|
2 |
|
public function encryptFileWithPassword($inputFilename, $outputFilename, $password) |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
2 |
|
return $this->encryption->encryptFileWithPassword($inputFilename, $outputFilename, $password); |
132
|
1 |
|
} catch (BaseCryptoException $ex) { |
133
|
1 |
|
throw new CryptoException($ex->getMessage()); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
* |
140
|
|
|
* @throws CryptoException |
141
|
|
|
*/ |
142
|
2 |
|
public function decryptFileWithPassword($inputFilename, $outputFilename, $password) |
143
|
|
|
{ |
144
|
|
|
try { |
145
|
2 |
|
return $this->encryption->decryptFileWithPassword($inputFilename, $outputFilename, $password); |
146
|
1 |
|
} catch (BaseCryptoException $ex) { |
147
|
1 |
|
throw new CryptoException($ex->getMessage()); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* {@inheritdoc} |
153
|
|
|
* |
154
|
|
|
* @throws CryptoException |
155
|
|
|
*/ |
156
|
2 |
|
public function encryptResourceWithKey($inputHandle, $outputHandle, KeyInterface $key) |
157
|
|
|
{ |
158
|
|
|
try { |
159
|
2 |
|
$this->encryption->encryptResourceWithKey($inputHandle, $outputHandle, $key); |
160
|
2 |
|
} catch (BaseCryptoException $ex) { |
161
|
1 |
|
throw new CryptoException($ex->getMessage()); |
162
|
|
|
} |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
* |
168
|
|
|
* @throws CryptoException |
169
|
|
|
*/ |
170
|
2 |
|
public function decryptResourceWithKey($inputHandle, $outputHandle, KeyInterface $key) |
171
|
|
|
{ |
172
|
|
|
try { |
173
|
2 |
|
return $this->encryption->decryptResourceWithKey($inputHandle, $outputHandle, $key); |
174
|
1 |
|
} catch (BaseCryptoException $ex) { |
175
|
1 |
|
throw new CryptoException($ex->getMessage()); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
* |
182
|
|
|
* @throws CryptoException |
183
|
|
|
*/ |
184
|
2 |
|
public function encryptResourceWithPassword($inputHandle, $outputHandle, $password) |
185
|
|
|
{ |
186
|
|
|
try { |
187
|
2 |
|
return $this->encryption->encryptResourceWithPassword($inputHandle, $outputHandle, $password); |
188
|
1 |
|
} catch (BaseCryptoException $ex) { |
189
|
1 |
|
throw new CryptoException($ex->getMessage()); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
* |
196
|
|
|
* @throws CryptoException |
197
|
|
|
*/ |
198
|
2 |
|
public function decryptResourceWithPassword($inputHandle, $outputHandle, $password) |
199
|
|
|
{ |
200
|
|
|
try { |
201
|
2 |
|
return $this->encryption->decryptResourceWithPassword($inputHandle, $outputHandle, $password); |
202
|
2 |
|
} catch (BaseCryptoException $ex) { |
203
|
1 |
|
throw new CryptoException($ex->getMessage()); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|