Encryptor   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 9
c 2
b 0
f 0
dl 0
loc 40
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A decryptData() 0 15 2
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