AES::getMode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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