Encryptor::decryptData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 10
cc 2
nc 2
nop 3
1
<?php
2
/**
3
 * Encryptor
4
 */
5
namespace tinymeng\OAuth2\Helper;
6
7
use tinymeng\OAuth2\Exception\OAuthException;
8
9
class Encryptor{
10
11
    /**
12
     * 解密数据(微信小程序手机号)
13
     * @param string $sessionKey
14
     * @param string $iv
15
     * @param string $encrypted
16
     * @return array
17
     * array(4) {
18
     *      ["phoneNumber"]=>
19
     *      string(11) "1314666****"
20
     *      ["purePhoneNumber"]=>
21
     *      string(11) "1314666****"
22
     *      ["countryCode"]=>
23
     *      string(2) "86"
24
     *      ["watermark"]=>
25
     *      array(2) {
26
     *      ["timestamp"]=>
27
     *      int(1732589884)
28
     *      ["appid"]=>
29
     *      string(18) "wxb771b4b7fb****"
30
     *      }
31
     * }
32
     * @throws OAuthException
33
     */
34
    static public function decryptData(string $sessionKey, string $iv, string $encrypted): array
35
    {
36
        $decrypted = AES::decrypt(
37
            base64_decode($encrypted, false),
38
            base64_decode($sessionKey, false),
39
            base64_decode($iv, false)
40
        );
41
42
        $decrypted = json_decode($decrypted, true);
43
44
        if (!$decrypted) {
45
            throw new OAuthException("The given payload is invalid.");
46
        }
47
48
        return $decrypted;
49
    }
50
}
51