AES   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 20.9 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 14
loc 67
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 7 7 1
A decrypt() 7 7 2
A getMode() 0 4 1
A validateKey() 0 6 2
A validateIv() 0 6 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kaylyu\Alipay\Kernel\Support;
4
5
/**
6
 * Class AES.
7
 */
8
class AES
9
{
10
    /**
11
     * @param string $text
12
     * @param string $key
13
     * @param string $iv
14
     * @param int    $option
15
     *
16
     * @return string
17
     */
18 View Code Duplication
    public static function encrypt(string $text, string $key, string $iv, int $option = OPENSSL_RAW_DATA): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        self::validateKey($key);
21
        self::validateIv($iv);
22
23
        return openssl_encrypt($text, self::getMode($key), $key, $option, $iv);
24
    }
25
26
    /**
27
     * @param string      $cipherText
28
     * @param string      $key
29
     * @param string      $iv
30
     * @param int         $option
31
     * @param string|null $method
32
     *
33
     * @return string
34
     */
35 View Code Duplication
    public static function decrypt(string $cipherText, string $key, string $iv, int $option = OPENSSL_RAW_DATA, $method = null): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        self::validateKey($key);
38
        self::validateIv($iv);
39
40
        return openssl_decrypt($cipherText, $method ?: self::getMode($key), $key, $option, $iv);
41
    }
42
43
    /**
44
     * @param string $key
45
     *
46
     * @return string
47
     */
48
    public static function getMode($key)
49
    {
50
        return 'aes-'.(8 * strlen($key)).'-cbc';
51
    }
52
53
    /**
54
     * @param string $key
55
     */
56
    public static function validateKey(string $key)
57
    {
58
        if (!in_array(strlen($key), [16, 24, 32], true)) {
59
            throw new \InvalidArgumentException(sprintf('Key length must be 16, 24, or 32 bytes; got key len (%s).', strlen($key)));
60
        }
61
    }
62
63
    /**
64
     * @param string $iv
65
     *
66
     * @throws \InvalidArgumentException
67
     */
68
    public static function validateIv(string $iv)
69
    {
70
        if (!empty($iv) && 16 !== strlen($iv)) {
71
            throw new \InvalidArgumentException('IV length must be 16 bytes.');
72
        }
73
    }
74
}
75