Passed
Push — master ( c48edd...c1a590 )
by Jean-Christophe
01:11
created

EncryptionManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
eloc 22
c 5
b 0
f 1
dl 0
loc 98
rs 10
wmc 12

10 Methods

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