Completed
Push — master ( af7ba6...67dcd9 )
by mingyoung
03:02
created

Encryptor::decryptData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 17
ccs 8
cts 10
cp 0.8
crap 3.072
rs 9.4285
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
/**
13
 * Encryptor.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\MiniProgram\Encryption;
28
29
use EasyWeChat\Encryption\EncryptionException;
30
use EasyWeChat\Encryption\Encryptor as BaseEncryptor;
31
use Exception;
32
33
class Encryptor extends BaseEncryptor
34
{
35
    /**
36
     * Decrypt data.
37
     *
38
     * @param string $sessionKey
39
     * @param string $iv
40
     * @param string $encrypted
41
     *
42
     * @return array
43
     */
44 1
    public function decryptData($sessionKey, $iv, $encrypted)
45
    {
46
        try {
47 1
            $decrypted = openssl_decrypt(
48 1
                base64_decode($encrypted, true), 'aes-128-cbc', base64_decode($sessionKey, true),
49 1
                OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, base64_decode($iv, true)
50 1
            );
51 1
        } catch (Exception $e) {
52
            throw new EncryptionException($e->getMessage(), EncryptionException::ERROR_DECRYPT_AES);
53
        }
54
55 1
        if (is_null($result = json_decode($this->decode($decrypted), true))) {
56
            throw new EncryptionException('ILLEGAL_BUFFER', EncryptionException::ILLEGAL_BUFFER);
57
        }
58
59 1
        return $result;
60
    }
61
}
62