AES256Crypter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 70
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 23 3
A decrypt() 0 23 3
A keyToEncryptionKey() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nastoletni\Code\Infrastructure;
6
7
use Nastoletni\Code\Application\Crypter\CrypterException;
8
use Nastoletni\Code\Application\Crypter\PasteCrypter;
9
use Nastoletni\Code\Domain\Paste;
10
11
class AES256Crypter implements PasteCrypter
12
{
13
    private const CIPHER = 'aes-256-cbc';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 2
    public function encrypt(Paste &$paste, string $key): void
19
    {
20 2
        $key = $this->keyToEncryptionKey($key);
21
22 2
        foreach ($paste->getFiles() as $file) {
23 2
            $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(static::CIPHER));
24
25 2
            $encrypted = openssl_encrypt(
26 2
                $file->getContent(),
27 2
                static::CIPHER,
28 2
                $key,
29 2
                0,
30 2
                $iv
31
            );
32 2
            $iv = base64_encode($iv);
33
34 2
            if (false === $encrypted) {
35
                throw new CrypterException('[OpenSSL] '.openssl_error_string());
36
            }
37
38 2
            $file->setContent($encrypted.':'.$iv);
39
        }
40 2
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function decrypt(Paste &$paste, string $key): void
46
    {
47 2
        $key = $this->keyToEncryptionKey($key);
48
49 2
        foreach ($paste->getFiles() as $file) {
50 2
            [$encrypted, $iv] = explode(':', $file->getContent());
0 ignored issues
show
Bug introduced by
The variable $encrypted does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $iv does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
51
52 2
            $iv = base64_decode($iv);
53 2
            $decrypted = openssl_decrypt(
54 2
                $encrypted,
55 2
                static::CIPHER,
56 2
                $key,
57 2
                0,
58 2
                $iv
59
            );
60
61 2
            if (false === $decrypted) {
62
                throw new CrypterException('[OpenSSL] '.openssl_error_string());
63
            }
64
65 2
            $file->setContent($decrypted);
66
        }
67 2
    }
68
69
    /**
70
     * Translates key that varies in length to 256bit (32 bytes) encryption key.
71
     *
72
     * @param string $key
73
     *
74
     * @return string
75
     */
76 2
    private function keyToEncryptionKey(string $key): string
77
    {
78 2
        return mb_substr(sha1($key, true), 0, 32);
79
    }
80
}
81