Completed
Push — master ( c26951...e01eb9 )
by Jared
02:09
created

Manager::getEncryptKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 10/08/2017
6
 * Time: 15:52
7
 */
8
9
namespace JC;
10
11
12
class Manager extends BaseManager
13
{
14
    /**
15
     * @var string
16
     */
17
    const ENCRYPT_METHOD = 'AES256';
18
19
    /**
20
     * @var string
21
     */
22
    public static $encryptKey;
23
24
    /**
25
     * @return string
26
     */
27
    public static function getCFileName()
28
    {
29
        return self::$cFileName ?: self::getUniqueString('encrypt');
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public static function getEncryptKey()
36
    {
37
        return self::$encryptKey ?: self::getUniqueString('encryptKey');
38
    }
39
40
    /**
41
     * @param string $encryptKey
42
     */
43
    public static function setEncryptKey($encryptKey)
44
    {
45
        self::$encryptKey = $encryptKey;
46
    }
47
48
    /**
49
     * @param $string
50
     * @return string
51
     */
52
    public static function encrypt($string)
53
    {
54
        return openssl_encrypt($string, self::ENCRYPT_METHOD, self::getEncryptKey(), 0, self::getIv());
55
    }
56
57
    /**
58
     * @param $string
59
     * @return string
60
     */
61
    public static function decrypt($string)
62
    {
63
        return openssl_decrypt($string, self::ENCRYPT_METHOD, self::getEncryptKey(), 0, self::getIv());
64
    }
65
66
    /**
67
     * @return bool|string
68
     */
69
    protected static function getIv()
70
    {
71
        return substr(md5(self::getEncryptKey()), 0, 16);
72
    }
73
74
    /**
75
     * @param $array
76
     * @return string
77
     */
78
    protected static function encode($array)
79
    {
80
        return self::encrypt(json_encode($array));
81
    }
82
83
    /**
84
     * @param $string
85
     * @return array
86
     */
87
    protected static function decode($string)
88
    {
89
        return json_decode(self::decrypt($string), true);
90
    }
91
92
}