Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

Encryption   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 1
b 0
f 0
dl 0
loc 58
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultData() 0 6 1
A decrypt() 0 3 2
A encrypt() 0 3 1
A getConfig() 0 10 2
1
<?php
2
/**
3
 * YetiForce register encryption file.
4
 * Modifying this file or functions that affect the footer appearance will violate the license terms!!!
5
 *
6
 * @package App
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
namespace App\YetiForce;
14
15
/**
16
 * YetiForce register class.
17
 */
18
final class Encryption
19
{
20
	/** @var string Default encryption method */
21
	public const DEFAULT_METHOD = 'AES-128-CBC';
22
23
	/**
24
	 * Data encryption function.
25
	 *
26
	 * @param array $data
27
	 *
28
	 * @return string
29
	 */
30
	public function encrypt(array $data): string
31
	{
32
		return (new \App\Encryption($this->getConfig()))->encrypt(\App\Json::encode($data), true);
33
	}
34
35
	/**
36
	 * Data decryption function.
37
	 *
38
	 * @param string $data
39
	 *
40
	 * @return array
41
	 */
42
	public function decrypt(string $data): array
43
	{
44
		return \App\Json::decode((new \App\Encryption($this->getConfig()))->decrypt($data, true)) ?: [];
45
	}
46
47
	/**
48
	 * Get default configuration data for encryption.
49
	 *
50
	 * @return array
51
	 */
52
	public function getDefaultData(): array
53
	{
54
		return [
55
			'method' => self::DEFAULT_METHOD,
56
			'vector' => base64_encode(openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::DEFAULT_METHOD))),
57
			'pass' => \App\Encryption::generatePassword(10),
58
		];
59
	}
60
61
	/**
62
	 * Configuration data for encryption.
63
	 *
64
	 * @return array
65
	 */
66
	private function getConfig(): array
67
	{
68
		$data = (new Config())->getData();
69
		unset($data['key']);
70
		$iv = $data['vector'] ?? '';
71
		if ($iv) {
72
			$data['vector'] = base64_decode($iv);
73
		}
74
75
		return $data;
76
	}
77
}
78