Completed
Push — master ( 7877b2...5132c9 )
by Francesco
03:15
created

Encryption::encryptWithPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 2
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));
0 ignored issues
show
Compatibility introduced by
$key of type object<Mes\Security\Cryp...dle\Model\KeyInterface> is not a sub-type of object<Mes\Security\CryptoBundle\Model\Key>. It seems like you assume a concrete implementation of the interface Mes\Security\CryptoBundle\Model\KeyInterface to be always present.

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.

Loading history...
Bug introduced by
It seems like $this->unlockKey($key) targeting Mes\Security\CryptoBundle\Encryption::unlockKey() can also be of type object<Defuse\Crypto\KeyProtectedByPassword>; however, Defuse\Crypto\Crypto::encrypt() does only seem to accept object<Defuse\Crypto\Key>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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));
0 ignored issues
show
Compatibility introduced by
$key of type object<Mes\Security\Cryp...dle\Model\KeyInterface> is not a sub-type of object<Mes\Security\CryptoBundle\Model\Key>. It seems like you assume a concrete implementation of the interface Mes\Security\CryptoBundle\Model\KeyInterface to be always present.

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.

Loading history...
Bug introduced by
It seems like $this->unlockKey($key) targeting Mes\Security\CryptoBundle\Encryption::unlockKey() can also be of type object<Defuse\Crypto\KeyProtectedByPassword>; however, Defuse\Crypto\Crypto::decrypt() does only seem to accept object<Defuse\Crypto\Key>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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));
0 ignored issues
show
Compatibility introduced by
$key of type object<Mes\Security\Cryp...dle\Model\KeyInterface> is not a sub-type of object<Mes\Security\CryptoBundle\Model\Key>. It seems like you assume a concrete implementation of the interface Mes\Security\CryptoBundle\Model\KeyInterface to be always present.

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.

Loading history...
Bug introduced by
It seems like $this->unlockKey($key) targeting Mes\Security\CryptoBundle\Encryption::unlockKey() can also be of type object<Defuse\Crypto\KeyProtectedByPassword>; however, Defuse\Crypto\File::encryptFile() does only seem to accept object<Defuse\Crypto\Key>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
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));
0 ignored issues
show
Compatibility introduced by
$key of type object<Mes\Security\Cryp...dle\Model\KeyInterface> is not a sub-type of object<Mes\Security\CryptoBundle\Model\Key>. It seems like you assume a concrete implementation of the interface Mes\Security\CryptoBundle\Model\KeyInterface to be always present.

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.

Loading history...
Bug introduced by
It seems like $this->unlockKey($key) targeting Mes\Security\CryptoBundle\Encryption::unlockKey() can also be of type object<Defuse\Crypto\KeyProtectedByPassword>; however, Defuse\Crypto\File::decryptFile() does only seem to accept object<Defuse\Crypto\Key>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
66 1
    }
67
68
    /**
69
     * Encrypts a plaintext string using a secret password.
70
     *
71
     * @param string $plaintext String to encrypt
72
     * @param string $password  String containing the secret password used for encryption
73
     *
74
     * @return string A ciphertext string representing $plaintext encrypted with a key derived from $password
75
     */
76
    public function encryptWithPassword($plaintext, $password)
77
    {
78
        // TODO: Implement encryptWithPassword() method.
79
    }
80
81
    /**
82
     * Decrypts a ciphertext string using a secret password.
83
     *
84
     * @param string $ciphertext ciphertext to be decrypted
85
     * @param string $password   A string containing the secret password used for decryption
86
     *
87
     * @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
88
     */
89
    public function decryptWithPassword($ciphertext, $password)
90
    {
91
        // TODO: Implement decryptWithPassword() method.
92
    }
93
94
    /**
95
     * Encrypts a file with a password.
96
     *
97
     * @param string $inputFilename  Path to a file containing the plaintext to encrypt
98
     * @param string $outputFilename Path to save the ciphertext file
99
     * @param string $password       The password used for decryption
100
     */
101
    public function encryptFileWithPassword($inputFilename, $outputFilename, $password)
102
    {
103
        // TODO: Implement encryptFileWithPassword() method.
104
    }
105
106
    /**
107
     * Decrypts a file with a password.
108
     *
109
     * @param string $inputFilename  Path to a file containing the ciphertext to decrypt
110
     * @param string $outputFilename Path to save the decrypted plaintext file
111
     * @param string $password       The password used for decryption
112
     */
113
    public function decryptFileWithPassword($inputFilename, $outputFilename, $password)
114
    {
115
        // TODO: Implement decryptFileWithPassword() method.
116
    }
117
118
    /**
119
     * @param Key|KeyInterface $key
120
     *
121
     * @return \Defuse\Crypto\Key|\Defuse\Crypto\KeyProtectedByPassword
122
     */
123 9
    private function unlockKey(Key $key)
124
    {
125 9
        return $key->unlock()
126 8
                   ->getRawKey();
127
    }
128
}
129