Completed
Push — master ( 32869d...8a9333 )
by Carlos
04:33 queued 30s
created

Encryptor   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 24
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A decryptData() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\MiniProgram;
13
14
use EasyWeChat\Kernel\Encryptor as BaseEncryptor;
15
use EasyWeChat\Kernel\Exceptions\DecryptException;
16
use EasyWeChat\Kernel\Support\AES;
17
18
/**
19
 * Class Encryptor.
20
 *
21
 * @author mingyoung <[email protected]>
22
 */
23
class Encryptor extends BaseEncryptor
24
{
25
    /**
26
     * Decrypt data.
27
     *
28
     * @param string $sessionKey
29
     * @param string $iv
30
     * @param string $encrypted
31
     *
32
     * @return array
33
     */
34 2
    public function decryptData(string $sessionKey, string $iv, string $encrypted): array
35
    {
36 2
        $decrypted = AES::decrypt(
37 2
            base64_decode($encrypted, false), base64_decode($sessionKey, false), base64_decode($iv, false)
38
        );
39
40 2
        $decrypted = json_decode($this->pkcs7Unpad($decrypted), true);
41
42 2
        if (!$decrypted) {
43 1
            throw new DecryptException('The given payload is invalid.');
44
        }
45
46 1
        return $decrypted;
47
    }
48
}
49