Crypt   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 46
c 0
b 0
f 0
dl 0
loc 137
ccs 43
cts 43
cp 1
rs 10
wmc 11

7 Methods

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