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\Crypto as BaseCrypto; |
15
|
|
|
use Defuse\Crypto\File as BaseCryptoFile; |
16
|
|
|
use Mes\Security\CryptoBundle\Model\Key; |
17
|
|
|
use Mes\Security\CryptoBundle\Model\KeyInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Encryption. |
21
|
|
|
*/ |
22
|
|
|
final class Encryption extends AbstractEncryption |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
* |
27
|
|
|
* @throw \Defuse\Crypto\Exception\EnvironmentIsBrokenException |
28
|
|
|
*/ |
29
|
2 |
|
public function encryptWithKey($plaintext, KeyInterface $key) |
30
|
|
|
{ |
31
|
2 |
|
return BaseCrypto::encrypt($plaintext, $this->unlockKey($key)); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
* |
37
|
|
|
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException |
38
|
|
|
* @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
39
|
|
|
*/ |
40
|
5 |
|
public function decryptWithKey($ciphertext, KeyInterface $key) |
41
|
|
|
{ |
42
|
5 |
|
return BaseCrypto::decrypt($ciphertext, $this->unlockKey($key)); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
* |
48
|
|
|
* @throws \Defuse\Crypto\Exception\IOException |
49
|
|
|
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException |
50
|
|
|
*/ |
51
|
1 |
|
public function encryptFileWithKey($inputFilename, $outputFilename, KeyInterface $key) |
52
|
|
|
{ |
53
|
1 |
|
BaseCryptoFile::encryptFile($inputFilename, $outputFilename, $this->unlockKey($key)); |
|
|
|
|
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* |
59
|
|
|
* @throws \Defuse\Crypto\Exception\IOException |
60
|
|
|
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException |
61
|
|
|
* @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException |
62
|
|
|
*/ |
63
|
1 |
|
public function decryptFileWithKey($inputFilename, $outputFilename, KeyInterface $key) |
64
|
|
|
{ |
65
|
1 |
|
BaseCryptoFile::decryptFile($inputFilename, $outputFilename, $this->unlockKey($key)); |
|
|
|
|
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Key|KeyInterface $key |
70
|
|
|
* |
71
|
|
|
* @return \Defuse\Crypto\Key|\Defuse\Crypto\KeyProtectedByPassword |
72
|
|
|
*/ |
73
|
9 |
|
private function unlockKey(Key $key) |
74
|
|
|
{ |
75
|
9 |
|
return $key->unlock() |
76
|
8 |
|
->getRawKey(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.