Completed
Push — master ( 9a6f5b...ef0055 )
by Pierre
02:26
created

Crypt::setAlgo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Component;
4
5
use App\Config;
6
7
/**
8
 * Lib_Auth_Crypt
9
 *
10
 * @author Pierre Fromager <pf@pier_infor.fr>
11
 * @version 1.0
12
 *
13
 * This is a static lib to encrypt/decrypt string from a given crypto algorithm.
14
 * Using openssl we check if the chosen algo is available.
15
 * We do not use mcrypt because deprecated and unsecure.
16
 */
17
18
class Crypt
19
{
20
21
    const BIT_8 = '8bit';
22
    const ERR_MSG_UNSUPPORTED_METHOD = 'Unsupported openssl method ';
23
    const ERR_MSG_ENCRYPTION_FAIL = 'Encryption failure';
24
    const DEFAULT_ALGO = 'aes-256-ctr';
25
26
    /**
27
     * method is crypt algo
28
     *
29
     * @var String
30
     */
31
    private $method = self::DEFAULT_ALGO;
32
33
    /**
34
     * crypt key
35
     *
36
     * @var String
37
     */
38
    private $key = '';
39
40
    /**
41
     * instanciate
42
     *
43
     * @param Config $config
44
     */
45 8
    public function __construct(Config $config)
46
    {
47 8
        $configJwt = $config->getSettings('jwt');
48 8
        $this->key = $configJwt['secret'];
49
    }
50
51
    /**
52
     * setAlgo
53
     *
54
     * @param string $algo
55
     * @return Crypt
56
     * @throws Exception
57
     */
58 2
    public function setAlgo(string $algo): Crypt
59
    {
60 2
        if (!in_array($algo, openssl_get_cipher_methods())) {
61 1
            throw new \Exception(self::ERR_MSG_UNSUPPORTED_METHOD . $algo);
62
        }
63 1
        $this->method = $algo;
64 1
        return $this;
65
    }
66
67
    /**
68
     * setB64Key
69
     *
70
     * @param string $key
71
     * @return Crypt
72
     * @throws Exception
73
     */
74 1
    public function setB64Key(string $key): Crypt
75
    {
76 1
        $this->key = base64_decode($key);
77 1
        return $this;
78
    }
79
80
    /**
81
     * encrypt content
82
     *
83
     * @param mixed $content
84
     * @param boolean $encode
85
     * @return mixed
86
     */
87 1
    public function encrypt($content, bool $encode = true)
88
    {
89 1
        $nonceSize = openssl_cipher_iv_length($this->method);
90 1
        $nonce = openssl_random_pseudo_bytes($nonceSize);
91 1
        $cryptedContent = openssl_encrypt(
92 1
            $content,
93 1
            $this->method,
94 1
            $this->key,
95 1
            OPENSSL_RAW_DATA,
96 1
            $nonce
97
        );
98 1
        if ($encode) {
99 1
            return base64_encode($nonce . $cryptedContent);
100
        }
101 1
        return $nonce . $cryptedContent;
102
    }
103
104
    /**
105
     * decrypt content
106
     *
107
     * @param mixed $content
108
     * @param boolean $encoded
109
     * @return mixed
110
     */
111 2
    public function decrypt($content, bool $encoded = true)
112
    {
113 2
        if ($encoded) {
114 2
            $content = @base64_decode($content, true);
115 2
            if ($content === false) {
116 1
                throw new \Exception(self::ERR_MSG_ENCRYPTION_FAIL);
117
            }
118
        }
119 1
        $nonceSize = openssl_cipher_iv_length($this->method);
120 1
        $nonce = mb_substr($content, 0, $nonceSize, self::BIT_8);
121 1
        $cryptedContent = mb_substr(
122 1
            $content,
123 1
            $nonceSize,
124 1
            null,
125 1
            self::BIT_8
126
        );
127 1
        $decrypted = openssl_decrypt(
128 1
            $cryptedContent,
129 1
            $this->method,
130 1
            $this->key,
131 1
            OPENSSL_RAW_DATA,
132 1
            $nonce
133
        );
134 1
        return $decrypted;
135
    }
136
137
    /**
138
     * getVersionNumber
139
     *
140
     * @return int
141
     */
142 1
    public function getVersionNumber(): int
143
    {
144 1
        return OPENSSL_VERSION_NUMBER;
145
    }
146
147
    /**
148
     * getVersionText
149
     *
150
     * @return string
151
     */
152 1
    public function getVersionText(): string
153
    {
154 1
        return OPENSSL_VERSION_TEXT;
155
    }
156
}
157