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
|
8 |
|
public function __construct(EncryptionInterface $encryption) |
35
|
|
|
{ |
36
|
8 |
|
$this->encryption = $encryption; |
37
|
8 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
* |
42
|
|
|
* @throw 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
|
|
|
* @throw 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
|
|
|
* @throw 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
|
|
|
* @throw 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
|
|
|
* Encrypts a plaintext string using a secret password. |
97
|
|
|
* |
98
|
|
|
* @param string $plaintext String to encrypt |
99
|
|
|
* @param string $password String containing the secret password used for encryption |
100
|
|
|
* |
101
|
|
|
* @return string A ciphertext string representing $plaintext encrypted with a key derived from $password |
102
|
|
|
*/ |
103
|
|
|
public function encryptWithPassword($plaintext, $password) |
104
|
|
|
{ |
105
|
|
|
// TODO: Implement encryptWithPassword() method. |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Decrypts a ciphertext string using a secret password. |
110
|
|
|
* |
111
|
|
|
* @param string $ciphertext ciphertext to be decrypted |
112
|
|
|
* @param string $password A string containing the secret password used for decryption |
113
|
|
|
* |
114
|
|
|
* @return string If the decryption succeeds, returns a string containing the same value as the string that was passed to encrypt() when $ciphertext was produced |
115
|
|
|
*/ |
116
|
|
|
public function decryptWithPassword($ciphertext, $password) |
117
|
|
|
{ |
118
|
|
|
// TODO: Implement decryptWithPassword() method. |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Encrypts a file with a password. |
123
|
|
|
* |
124
|
|
|
* @param string $inputFilename Path to a file containing the plaintext to encrypt |
125
|
|
|
* @param string $outputFilename Path to save the ciphertext file |
126
|
|
|
* @param string $password The password used for decryption |
127
|
|
|
*/ |
128
|
|
|
public function encryptFileWithPassword($inputFilename, $outputFilename, $password) |
129
|
|
|
{ |
130
|
|
|
// TODO: Implement encryptFileWithPassword() method. |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Decrypts a file with a password. |
135
|
|
|
* |
136
|
|
|
* @param string $inputFilename Path to a file containing the ciphertext to decrypt |
137
|
|
|
* @param string $outputFilename Path to save the decrypted plaintext file |
138
|
|
|
* @param string $password The password used for decryption |
139
|
|
|
*/ |
140
|
|
|
public function decryptFileWithPassword($inputFilename, $outputFilename, $password) |
141
|
|
|
{ |
142
|
|
|
// TODO: Implement decryptFileWithPassword() method. |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|