EncryptionManager::encrypt()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
namespace Ubiquity\security\data;
3
4
use Ubiquity\config\Configuration;
0 ignored issues
show
Bug introduced by
The type Ubiquity\config\Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Ubiquity\config\EnvFile;
0 ignored issues
show
Bug introduced by
The type Ubiquity\config\EnvFile was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Ubiquity\controllers\Startup;
7
8
/**
9
 * Ubiquity\security\data$EncryptionManager
10
 * This class is part of Ubiquity
11
 *
12
 * @author jc
13
 * @version 1.0.2
14
 *
15
 */
16
class EncryptionManager {
17
18
	const ENCRYPTION_KEY_NAME = 'encryption_key';
19
20
	/**
21
	 *
22
	 * @var Encryption
23
	 */
24
	private static $encryptionInstance;
25
26
	private static function getInstance(?string $key, ?string $cypher = Encryption::AES128): Encryption {
27
		return self::$encryptionInstance ??= new Encryption($key, $cypher);
28
	}
29
30
	/**
31
	 * Start the manager and generate the encryption key.
32
	 * Do not use in production
33
	 *
34
	 * @param array $config
35
	 * @param ?string $cipher
36
	 */
37
	public static function start(array &$config, ?string $cipher = Encryption::AES128) {
38
		$oldKey = $config[self::ENCRYPTION_KEY_NAME] ?? null;
39
		self::getInstance($oldKey, $cipher);
40
		self::$encryptionInstance->initializeKeyAndCipher();
41
		$key = self::$encryptionInstance->getKey();
42
43
		if ($oldKey !== $key) {
44
			$oConfig = Configuration::loadConfigWithoutEval('config');
45
			$oConfig[self::ENCRYPTION_KEY_NAME] = "getenv('".self::ENCRYPTION_KEY_NAME."')";
46
			$envArray=EnvFile::load();
47
			$envArray[self::ENCRYPTION_KEY_NAME]=$key;
48
			EnvFile::save($envArray);
49
			Startup::saveConfig($oConfig);
50
		}
51
	}
52
53
	/**
54
	 * Start the encryption manager for production.
55
	 *
56
	 * @param array $config
57
	 * @param ?string $cypher
58
	 */
59
	public static function startProd(array $config, ?string $cypher = null) {
60
		$key = $config[self::ENCRYPTION_KEY_NAME];
61
		self::getInstance($key, $cypher ?? Encryption::getCipherFromKey($key));
62
	}
63
64
	/**
65
	 * Encrypt the given data.
66
	 *
67
	 * @param mixed $data
68
	 * @return string
69
	 */
70
	public static function encrypt($data): string {
71
		if (is_string($data)) {
72
			return self::$encryptionInstance->encryptString($data);
73
		}
74
		return self::$encryptionInstance->encrypt($data);
75
	}
76
77
	/**
78
	 * Decrypt the given string.
79
	 *
80
	 * @param string $data
81
	 * @return string
82
	 */
83
	public static function decryptString(string $data): string {
84
		return self::$encryptionInstance->decryptString($data);
85
	}
86
87
	/**
88
	 * Decrypt the given data with possible unserialization.
89
	 *
90
	 * @param string $data
91
	 * @param boolean $unserialize
92
	 * @return mixed|string
93
	 */
94
	public static function decrypt(string $data, $unserialize = true) {
95
		return self::$encryptionInstance->decrypt($data, $unserialize);
96
	}
97
98
	/**
99
	 * Generate a new encryption key.
100
	 *
101
	 * @param string $cipher
102
	 * @return string
103
	 */
104
	public static function generateKey(?string $cipher = Encryption::AES128): string {
105
		return self::getInstance(null)->generateKey($cipher ?? Encryption::AES128);
106
	}
107
108
	public static function getKey() {
109
		return self::getInstance(self::getKey())->getKey();
110
	}
111
112
	public static function getEncryptionInstance(): ?Encryption {
113
		return self::$encryptionInstance;
114
	}
115
116
	public static function isStarted(): bool {
117
		return isset(self::$encryptionInstance);
118
	}
119
}
120
121