Completed
Push — master ( e64f37...ec241f )
by Jared
02:07
created

EncryptManager::getIv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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 EncryptManager extends CacheManager
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 getEncryptKey()
28
    {
29
        return self::$encryptKey;
30
    }
31
32
    /**
33
     * @param string $encryptKey
34
     */
35
    public static function setEncryptKey($encryptKey)
36
    {
37
        self::$encryptKey = $encryptKey;
38
    }
39
40
    /**
41
     * @param $string
42
     * @return string
43
     */
44
    public static function encrypt($string)
45
    {
46
        return openssl_encrypt($string, self::ENCRYPT_METHOD, self::getEncryptKey(), 0, self::getIv());
47
    }
48
49
    /**
50
     * @param $string
51
     * @return string
52
     */
53
    public static function decrypt($string)
54
    {
55
        return openssl_decrypt($string, self::ENCRYPT_METHOD, self::getEncryptKey(), 0, self::getIv());
56
    }
57
58
    /**
59
     * @return bool|string
60
     */
61
    protected static function getIv()
62
    {
63
        return substr(md5(self::getEncryptKey()), 0, 16);
64
    }
65
66
    /**
67
     * @param $array
68
     * @return string
69
     */
70
    protected static function encode($array)
71
    {
72
        return self::encrypt(json_encode($array));
73
    }
74
75
    /**
76
     * @param $string
77
     * @return array
78
     */
79
    protected static function decode($string)
80
    {
81
        return json_decode(self::decrypt($string), true);
82
    }
83
84
}