Total Complexity | 7 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
5 | class Encrypter |
||
6 | { |
||
7 | private $method = 'aes-128-ecb'; |
||
8 | private $key; |
||
9 | private $prefix; |
||
10 | |||
11 | /** |
||
12 | * @param $value |
||
13 | * @return string |
||
14 | */ |
||
15 | public function encrypt($value) |
||
16 | { |
||
17 | return $this->getPrefix() . '_' . openssl_encrypt($value, $this->method, $this->getKey(), 0, $iv = ''); |
||
|
|||
18 | } |
||
19 | |||
20 | /** |
||
21 | * @param $value |
||
22 | * @return string |
||
23 | */ |
||
24 | public function decrypt($value) |
||
25 | { |
||
26 | $value = str_replace("{$this->getPrefix()}_",'',$value); |
||
27 | |||
28 | return openssl_decrypt($value, $this->method, $this->getKey(), 0, $iv = ''); |
||
29 | } |
||
30 | |||
31 | |||
32 | /** |
||
33 | * @return bool|null|string |
||
34 | * @throws \Exception |
||
35 | */ |
||
36 | public function getKey() |
||
37 | { |
||
38 | if ($this->key === null) { |
||
39 | if(! Config::get('encrypt.key')) throw new \Exception('The .env value ENCRYPT_KEY has to be set'); |
||
40 | $this->key = substr(hash('sha256', Config::get('encrypt.key')), 0, 16); |
||
41 | } |
||
42 | return $this->key; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return mixed |
||
47 | */ |
||
48 | public function getPrefix() |
||
54 | } |
||
55 | |||
56 | } |