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
|
8 |
|
public function __construct(EncryptionInterface $encryption) |
30
|
|
|
{ |
31
|
8 |
|
$this->encryption = $encryption; |
32
|
8 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* |
37
|
|
|
* @throw CryptoException |
38
|
|
|
*/ |
39
|
2 |
|
public function encryptWithKey($plaintext, KeyInterface $key) |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
2 |
|
return $this->encryption->encryptWithKey($plaintext, $key); |
43
|
1 |
|
} catch (EnvironmentIsBrokenException $ex) { |
44
|
1 |
|
throw new CryptoException($ex->getMessage()); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
* |
51
|
|
|
* @throw CryptoException |
52
|
|
|
*/ |
53
|
2 |
|
public function decryptWithKey($ciphertext, KeyInterface $key) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
2 |
|
return $this->encryption->decryptWithKey($ciphertext, $key); |
57
|
1 |
|
} catch (BaseCryptoException $ex) { |
58
|
1 |
|
throw new CryptoException($ex->getMessage()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
* |
65
|
|
|
* @throw CryptoException |
66
|
|
|
*/ |
67
|
2 |
|
public function encryptFileWithKey($inputFilename, $outputFilename, KeyInterface $key) |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
2 |
|
$this->encryption->encryptFileWithKey($inputFilename, $outputFilename, $key); |
71
|
2 |
|
} catch (BaseCryptoException $ex) { |
72
|
1 |
|
throw new CryptoException($ex->getMessage()); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
* |
79
|
|
|
* @throw CryptoException |
80
|
|
|
*/ |
81
|
2 |
|
public function decryptFileWithKey($inputFilename, $outputFilename, KeyInterface $key) |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
2 |
|
$this->encryption->decryptFileWithKey($inputFilename, $outputFilename, $key); |
85
|
2 |
|
} catch (BaseCryptoException $ex) { |
86
|
1 |
|
throw new CryptoException($ex->getMessage()); |
87
|
|
|
} |
88
|
1 |
|
} |
89
|
|
|
} |
90
|
|
|
|