Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

Crypt::setAlgo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace App\Tools;
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
    public function __construct(Config $config)
46
    {
47
        $configJwt = $config->getSettings('jwt');
48
        $this->key = $configJwt['secret'];
49
    }
50
51
    /**
52
     * setAlgo
53
     *
54
     * @param string $algo
55
     * @return Crypt
56
     * @throws Exception
57
     */
58
    public function setAlgo(string $algo): Crypt
59
    {
60
        if (!in_array($algo, openssl_get_cipher_methods())) {
61
            throw new \Exception(self::ERR_MSG_UNSUPPORTED_METHOD . $algo);
62
        }
63
        $this->method = $algo;
64
        return $this;
65
    }
66
67
    /**
68
     * setB64Key
69
     *
70
     * @param string $key
71
     * @return Crypt
72
     * @throws Exception
73
     */
74
    public function setB64Key(string $key): Crypt
75
    {
76
        $this->key = base64_decode($key);
77
        return $this;
78
    }
79
80
    /**
81
     * encrypt content
82
     *
83
     * @param mixed $content
84
     * @param boolean $encode
85
     * @return mixed
86
     */
87
    public function encrypt($content, bool $encode = true)
88
    {
89
        $nonceSize = openssl_cipher_iv_length($this->method);
90
        $nonce = openssl_random_pseudo_bytes($nonceSize);
91
        $cryptedContent = openssl_encrypt(
92
            $content,
93
            $this->method,
94
            $this->key,
95
            OPENSSL_RAW_DATA,
96
            $nonce
97
        );
98
        if ($encode) {
99
            return base64_encode($nonce . $cryptedContent);
100
        }
101
        return $nonce . $cryptedContent;
102
    }
103
104
    /**
105
     * decrypt content
106
     *
107
     * @param mixed $content
108
     * @param boolean $encoded
109
     * @return mixed
110
     */
111
    public function decrypt($content, bool $encoded = true)
112
    {
113
        if ($encoded) {
114
            $content = @base64_decode($content, true);
115
            if ($content === false) {
116
                throw new \Exception(self::ERR_MSG_ENCRYPTION_FAIL);
117
            }
118
        }
119
        $nonceSize = openssl_cipher_iv_length($this->method);
120
        $nonce = mb_substr($content, 0, $nonceSize, self::BIT_8);
121
        $cryptedContent = mb_substr(
122
            $content,
123
            $nonceSize,
124
            null,
125
            self::BIT_8
126
        );
127
        $decrypted = openssl_decrypt(
128
            $cryptedContent,
129
            $this->method,
130
            $this->key,
131
            OPENSSL_RAW_DATA,
132
            $nonce
133
        );
134
        return $decrypted;
135
    }
136
137
    /**
138
     * getVersionNumber
139
     *
140
     * @return int
141
     */
142
    public function getVersionNumber(): int
143
    {
144
        return OPENSSL_VERSION_NUMBER;
145
    }
146
147
    /**
148
     * getVersionText
149
     *
150
     * @return string
151
     */
152
    public function getVersionText(): string
153
    {
154
        return OPENSSL_VERSION_TEXT;
155
    }
156
}
157